add backend source code
This commit is contained in:
31
backend/rebuild_db.py
Normal file
31
backend/rebuild_db.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""重建数据库"""
|
||||
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())
|
||||
Reference in New Issue
Block a user