是否不必在任何事务中删除数据库? [英] Does dropping a database have to be done not in any transaction?

查看:172
本文介绍了是否不必在任何事务中删除数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 https://wiki.postgresql.org/wiki/Psycopg2_Tutorial


PostgreSQL无法在事务中删除数据库,这是一个全
或全无的命令。如果要删除数据库,则需要
更改数据库的隔离级别,可使用以下
来完成。

PostgreSQL can not drop databases within a transaction, it is an all or nothing command. If you want to drop the database you would need to change the isolation level of the database this is done using the following.

conn.set_isolation_level(0)

您应将以上内容直接放在DROP DATABASE
游标执行。

You would place the above immediately preceding the DROP DATABASE cursor execution.

为什么如果要删除数据库,则需要更改隔离级别数据库?
特别是为什么我们需要将隔离级别更改为0? (如果我是正确的,则0表示 psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED

Why "If you want to drop the database you would need to change the isolation level of the database"? In particular, why do we need to change the isolation level to 0? (If I am correct, 0 means psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)

来自https://stackoverflow.com/a/51859484/156458


销毁数据库的操作以
防止其撤消的方式实现-因此您不能从
事务内部运行它,因为事务始终是不可撤消的。
请记住,与大多数其他数据库不同,PostgreSQL允许几乎所有DDL
语句(显然不是DROP DATABASE语句)都可以在
a事务内执行。

The operation of destroying a database is implemented in a way which prevents undoing it - therefore you can not run it from inside a transaction because transactions are always undoable. Also keep in mind that unlike most other databases PostgreSQL allows almost all DDL statements (obviously not the DROP DATABASE one) to be executed inside a transaction.

实际上,如果任何人(包括您)当前已连接到数据库
,则您不能删除该数据库-因此,隔离级别
无关紧要,您仍然必须连接到另一个数据库
(例如postgres)

Actually you can not drop a database if anyone (including you) is currently connected to this database - so it does not matter what is your isolation level, you still have to connect to another database (e.g. postgres)

您不能从tra内部运行它nsaction,因为事务总是不可撤销的。那我怎么不从事务内部删除数据库呢?

"you can not run it from inside a transaction because transactions are always undoable". Then how can I drop a database not from inside a transaction?

我在 https://stackoverflow.com/a/51880577/156458

推荐答案

我对psycopg2不熟悉,所以我只能提供要执行的步骤。

I'm unfamiliar with psycopg2 so I can only provide steps to be performed.

执行 DROP的步骤来自 Python 的数据库

Steps to be taken to perform DROP DATABASE from Python:


  1. 连接到其他数据库,而您不会t要删除

  2. 将当前隔离级别存储在变量中

  3. 将隔离级别设置为0

  4. 执行 DROP DATABASE 查询

  5. 将隔离级别设置回原始级别(从#2开始)

  1. Connect to a different database, which you don't want to drop
  2. Store current isolation level in a variable
  3. Set isolation level to 0
  4. Execute DROP DATABASE query
  5. Set isolation level back to original (from #2)






PSQL执行 DROP DATABASE 的步骤strong>:


Steps to be taken to perform DROP DATABASE from PSQL:


  1. 连接到您不想删除的其他数据库

  2. 执行 DROP D ATABASE 查询

  1. Connect to a different database, which you don't want to drop
  2. Execute DROP DATABASE query

psql中的代码

\c second_db
DROP DATABASE first_db;






请记住,不能有任何实时连接您要删除的数据库。


Remember, that there can be no live connections to the database you are trying to drop.

这篇关于是否不必在任何事务中删除数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆