PostgreSQL - Rename database

后端 未结 6 857
感情败类
感情败类 2020-12-23 02:27

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 c

6条回答
  •  甜味超标
    2020-12-23 03:15

    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.

提交回复
热议问题