简体   繁体   English

PostgreSQL - 重命名数据库

[英]PostgreSQL - Rename database

I need to rename the database but when I do in PGAdmin : ALTER DATABASE "databaseName" RENAME TO "databaseNameOld" it told me that it cannot.我需要重命名数据库,但是当我在PGAdmin : ALTER DATABASE "databaseName" RENAME TO "databaseNameOld"这样做时PGAdmin : ALTER DATABASE "databaseName" RENAME TO "databaseNameOld"它告诉我它不能。

How can I do it?我该怎么做?

( Version 8.3 on WindowsXP ) WindowsXP 8.3 版

Update更新

  • The first error message : Cannot because I was connect to it.第一条错误消息:无法连接,因为我已连接到它。 So I selected an other database and did the queries.所以我选择了另一个数据库并进行了查询。

  • I get a second error message telling me that it has come user connect.我收到第二条错误消息,告诉我它已进入用户连接。 I see in the PGAdmin screen that it has many PID but they are inactive... I do not see how to kill them.我在PGAdmin屏幕中看到它有很多PID但它们处于非活动状态...我不知道如何杀死它们。

Try not quoting the database name:尽量不要引用数据库名称:

ALTER DATABASE people RENAME TO customers;

Also ensure that there are no other clients connected to the database at the time.还要确保当时没有其他客户端连接到数据库。 Lastly, try posting the error message it returns so we can get a bit more information.最后,尝试发布它返回的错误消息,以便我们可以获得更多信息。

For future reference, you should be able to:为了将来参考,您应该能够:

-- disconnect from the database to be renamed
\c postgres

-- force disconnect all other clients from the database to be renamed
SELECT pg_terminate_backend( pid )
FROM pg_stat_activity
WHERE pid <> pg_backend_pid( )
    AND datname = 'name of database';

-- rename the database (it should now have zero clients)
ALTER DATABASE "name of database" RENAME TO "new name of database";

Note that table pg_stat_activity column pid was named as procpid in versions prior to 9.2.请注意,在 9.2 之前的版本中,表pg_stat_activitypid被命名为procpid So if your PostgreSQL version is lower than 9.2, use procpid instead of pid .因此,如果您的 PostgreSQL 版本低于 9.2,请使用procpid而不是pid

I just ran into this and below is what worked:我刚刚遇到了这个,下面是有效的:

1) pgAdmin is one of the sessions. 1) pgAdmin是会话之一。 Use psql instead.请改用psql
2) Stop the pgBouncer and/or scheduler services on Windows as these also create sessions 2) 停止 Windows 上的pgBouncer和/或调度程序服务,因为它们也会创建会话

Unexist told me in comment to restart the database and it works! Unexist 在评论中告诉我重新启动数据库,它可以工作! Restarting the database kill all existing connection and then I connect to an other database and was able to rename it with my initial query.重新启动数据库会杀死所有现有连接,然后我连接到另一个数据库并能够使用我的初始查询重命名它。

Thx all.谢谢所有。

您应该尝试关闭那些打扰您的连接,而不是部署 nuke(重新启动服务器),方法是查找它们来自何处并关闭客户端进程或使用pg_cancel_backend()函数。

For anyone running into this issue using DBeaver and getting an error message like this:对于使用 DBeaver 遇到此问题并收到如下错误消息的任何人:

ERROR: database "my_stubborn_db" is being accessed by other users
  Detail: There is 1 other session using the database.

Disconnect your current connection, and reconnect to the same server with a connection that doesn't target the database you are renaming.断开当前连接,并使用不针对您正在重命名的数据库的连接重新连接到同一台服务器。

Changing the active database is not enough.更改活动数据库是不够的。

When connected via pgadmin, the default database will be postgres.通过 pgadmin 连接时,默认数据库将是 postgres。

ALTER DATABASE postgres RENAME TO pgnew;

This will not work.这是行不通的。

You need to right click on server in pgadmin and set Maintenance DB to some other DB and save.您需要在 pgadmin 中右键单击服务器并将维护数据库设置为其他数据库并保存。 Then retry and it should work if no other connections exists.然后重试,如果不存在其他连接,它应该可以工作。

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

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