165

I need to rename the database but when I do in PGAdmin : ALTER DATABASE "databaseName" RENAME TO "databaseNameOld" it told me that it cannot.

How can I do it?

(Version 8.3 on WindowsXP)

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.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341

9 Answers9

226

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.

bmdhacks
  • 15,841
  • 8
  • 34
  • 55
114

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. So if your PostgreSQL version is lower than 9.2, use procpid instead of pid.

Mohayemin
  • 3,841
  • 4
  • 25
  • 54
gsiems
  • 3,500
  • 1
  • 22
  • 24
  • 3
    it worked for me, thanx ! but the column name in pg_stat_activity is pid and not procpid. (`PostgreSQL 9.3.5 on x86_64-apple-darwin, compiled by i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00), 64-bit`) – 31 bit Feb 10 '15 at 17:09
  • The force disconnect code is extremely useful and should be in the accepted answer. – Jedidja Mar 08 '21 at 14:08
8

I just ran into this and below is what worked:

1) pgAdmin is one of the sessions. Use psql instead.
2) Stop the pgBouncer and/or scheduler services on Windows as these also create sessions

smoore4
  • 4,520
  • 3
  • 36
  • 55
3

Unexist told me in comment to restart the database and it works! 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.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
3

Instead of deploying a nuke (restarting the server) you should try to close those connections that bother you either by finding where are they from and shutting down the client processes or by using the pg_cancel_backend() function.

Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
2
ALTER DATABASE old_database RENAME TO new_database;

old_database means already existing database. new_database means need to modify this name.

Example: ALTER DATABASE profile RENAME TO address;
1

When connected via pgadmin, the default database will be 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. Then retry and it should work if no other connections exists.

Valsaraj Viswanathan
  • 1,473
  • 5
  • 28
  • 51
0

For anyone running into this issue using DBeaver and getting an error message like this:

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.

rovyko
  • 4,068
  • 5
  • 32
  • 44
0

from the default postgres db login with admin and then execute ALTER DATABASE old_database RENAME TO new_database; it will work.