简体   繁体   English

在 AWS RDS 实例上删除数据库 - Postgres

[英]Drop DB on AWS RDS instance - Postgres

Trying to drop a db from AWS RDS (postgres) is blocked only for superuser (which is reserved to AWS only).尝试从 AWS RDS (postgres) 删除数据库仅被超级用户阻止(仅保留给 AWS)。

trying to run this command on my instance:尝试在我的实例上运行此命令:

DROP DATABASE "dbname"

results in error:导致错误:

psycopg2.InternalError: DROP DATABASE cannot run inside a transaction block

I saw this issue in AWS forums, which seems to have been active by a lot of people, but no AWS representative giving a valid solution.我在 AWS 论坛上看到了这个问题,似乎很多人都在讨论这个问题,但没有 AWS 代表给出有效的解决方案。

How can I drop my db without taking down the whole instance and raising it again?如何在不删除整个实例并再次提升它的情况下删除我的数据库?

Try using ISOLATION_LEVEL_AUTOCOMMIT , a psycopg2 extensions:尝试使用ISOLATION_LEVEL_AUTOCOMMIT ,一个 psycopg2 扩展:

No transaction is started when command are issued and no commit() or rollback() is required.发出命令时不会启动任何事务,并且不需要 commit() 或 rollback()。

The connection must be in autocommit mode.连接必须处于自动提交模式。 You can way can set it using psycopg2 is through the autocommit attribute:您可以通过自动提交属性使用 psycopg2 设置它:

import psycopg2

con = psycopg2.connect(...)
con.autocommit = True

cur = con.cursor()
cur.execute('DROP DATABASE db;')

Worked for me:为我工作:

  1. Connect to postgres DB, disconnect from DB you want to DROP!连接到postgres数据库,断开你想要 DROP 的数据库!
  2. select pg_terminate_backend(pid) from pg_stat_activity where datname='yourdb';
  3. drop database 'yourdb';

Step 2 and 3 execute fast in order to prevent user rdsadmin reconnect快速执行第 2 步和第 3 步以防止用户rdsadmin重新连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM