Files
hospital_performance/backend/rebuild_db.py
2026-02-28 15:06:52 +08:00

32 lines
864 B
Python

"""重建数据库"""
import asyncio
import asyncpg
async def main():
# 连接到 postgres 数据库
conn = await asyncpg.connect(
'postgresql://postgresql:Jchl1528@192.168.110.252:15432/postgres'
)
try:
# 终止所有连接
await conn.execute("""
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'hospital_performance' AND pid <> pg_backend_pid()
""")
# 删除数据库
await conn.execute('DROP DATABASE IF EXISTS hospital_performance')
print('数据库已删除')
# 重新创建
await conn.execute('CREATE DATABASE hospital_performance')
print('数据库已创建')
finally:
conn.terminate()
if __name__ == '__main__':
asyncio.run(main())