-- 将数据库设置为不允许新连接
UPDATE pg_database SET datallowconn = 'true' WHERE datname = 'old_dbname';
-- 终止所有与数据库相关的活动连接
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'new_dbname';
-- 删除数据库
DROP DATABASE old_dbname;
ALTER DATABASE old_dbname RENAME TO new_dbname;
务必在执行这些步骤之前备份数据库,以防不可预见的问题。