>>> import subprocess, sqlalchemy
>>> engine = sqlalchemy.create_engine("postgresql+psycopg://myuser:secret@localhost/mydb")
>>> subprocess.Popen(['/usr/bin/dropdb', '-U', 'myuser', 'mydb'])
dropdb: database removal failed: ERROR: database "openstack_citest" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
我检查了哪个用户正在使用该数据库,正是该用户试图删除它。
我可以通过发出 engine.dispose()
调用来解决这个问题,但我不知道为什么。首先不应该有任何与数据库的打开连接。
尝试使用 mysql
进行相同的操作,因为后端无需调用 dispose()
即可正常删除数据库。
最佳答案
作者:design :
DROP DATABASE drops a database. [...]. Also, it cannot be executed while you or anyone else are connected to the target database
关于python - 为什么我需要处置 postgresql sqlalchemy 引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14258680/