Last Updated : 15 Jul, 2025
Renaming a PostgreSQL database is a simple yet essential task for developers and database administrators. Whether we're migrating data, restructuring our database, or just managing our server efficiently, the ability to rename a PostgreSQL database can streamline our workflow.
In this article, we will walk through the steps on how to rename a PostgreSQL database efficiently, both through the command line and using tools like cPanel, ensuring a seamless process for developers and database administrators.
PostgreSQL Rename DatabaseRenaming a PostgreSQL database can be a straightforward operation but requires careful handling, especially when active connections are involved. The process involves disconnecting from the database, terminating active sessions, and using the ALTER DATABASE statement to change the database name.
This process ensures that the database is correctly renamed without causing any disturbance to the underlying data or performance. Understanding the PostgreSQL rename database procedure is essential for maintaining a clean and organized database environment.
Syntax
ALTER DATABASE old_db_name RENAME TO new_db_name;
Key Terms
old_db_name
is the current name of the database.new_db_name
is the desired new name of the database.Renaming a PostgreSQL database may be necessary in various scenarios, such as:
In PostgreSQL, the ALTER DATABASE RENAME TO statement is used to rename a database. The below steps need to be followed while renaming a database:
Now let's look into the below example to see how to rename a database in PostgreSQL. This will help us understand the process step by step and ensure a smooth renaming operation.
Step 1: Create a DatabaseCreate a database named "test_db" using the below commands. This will create a new database named test_db. Now, let’s rename this database.
Query:
CREATE DATABASE test_db;
Output
Step 2: Disconnect from the DatabaseNow to rename the "test_db" database, disconnect from that database using the below command and connect to the Postgres database:
Query:
test_db=# \connect postgres;
Output
Step 3: Check Active ConnectionsBefore renaming the database, we need to ensure there are no active connections to test_db
. Use the following query to see all connections to the test_db
database:
Query:
SELECT
*
FROM
pg_stat_activity
WHERE
datname = 'test_db';
Output
Step 4: Terminate Active ConnectionsNow, terminate any active connections to test_db
using the pg_terminate_backend
function:
Query:
SELECT
pg_terminate_backend (pid)
FROM
pg_stat_activity
WHERE
datname = 'test_db';
Output
Step 5: Rename the DatabaseNow use the ALTER DATABASE RENAME TO statement to rename the database as "new_test_db"(say) as follows:
Query:
ALTER DATABASE test_db RENAME TO new_test_db;
Output
ALTER DATABASE RENAME TO ConclusionIn conclusion, the PostgreSQL rename command using the ALTER DATABASE RENAME TO statement is an efficient way to rename a database in PostgreSQL. By following the PostgreSQL database rename process, we can quickly change the name of our databases with minimal disruption. Always ensure that we are not connected to the target database and that all active connections are terminated before proceeding.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4