python - 为什么我需要处置 postgresql sqlalchemy 引擎?

标签 python postgresql sqlalchemy

>>> 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/

相关文章:

java - 打包数据到 byte[]

python - python函数的奇怪行为

ruby-on-rails - PG::ConnectionBad: FATAL: Peer authentication failed for user 'username' 错误

postgresql - 对在 postgresql 中使用 array_agg 创建的文本聚合进行排序

spring - 在 CRON JOBS 中执行方法

python - 如何使用 Python 子进程在 Windows 中进行搜索

python - 将数字与字母分开的正则表达式

python - SQLAlchemy:为什么附加到这种多对一关系会失败?

python - 当表列更改时如何更新 orm+python 中的数据库字段?

python - 使用 SQLAlchemy 进行 Pyramid 异常记录 - 命令未提交