Made a typo in a database name, or need a better one? You can rename a database in PostgreSQL with one statement, as long as nobody is connected to it.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
ALTER DATABASE name RENAME TO new_name;
name: the database’s current name.new_name: the new name. It follows the usual rules for names, so lowercase letters, digits and underscores are the safe choice.- You must own the database or be a superuser, such as
postgres. An owner who isn’t a superuser also needs theCREATEDBprivilege. - You can’t rename the database you’re connected to, and nobody else may be connected to it either.
This is one of the forms of ALTER DATABASE (opens in a new tab) in the PostgreSQL documentation.
Step 1: Connect to Another Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
This guide renames a database called inventry, a typo, to inventory. To follow along, create it first:
CREATE DATABASE inventry;
CREATE DATABASE
Step 2: Rename the Database
ALTER DATABASE inventry RENAME TO inventory;
ALTER DATABASE
psql answers ALTER DATABASE: the database is now called inventory. Only the name changes; everything inside it stays as it was.
Clean Up
To remove the example database:
DROP DATABASE inventory;
DROP DATABASE
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Renaming the database you’re connected to | ERROR: current database cannot be renamed | Connect to another database first: \c postgres. |
| Renaming while someone else is connected, such as pgAdmin or another psql window | ERROR: database "inventory" is being accessed by other users, with DETAIL: There is 1 other session using the database. | Close the other connection first: quit the other psql window with \q, or in pgAdmin right-click the database and choose Disconnect from database. |
| A name that’s already taken | ERROR: database "postgres" already exists | Pick a name no other database on the server uses. |
FAQ
Do I need to change anything after renaming?
Yes. Anything that connects to the database by name, such as a connection string, an app’s settings or a saved connection in pgAdmin, must use the new name.
Can I undo a rename?
Yes. Rename it back with the same statement, with the two names swapped.
Conclusion
Connect to another database, make sure nobody else is connected to the one you’re renaming, and rename it with ALTER DATABASE … RENAME TO. Only the name changes.
Additional Resources
- PostgreSQL 19 documentation: ALTER DATABASE (opens in a new tab)
- PostgreSQL 19 documentation: Identifiers and Key Words (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 4: Create a Database
- How to Drop a Database in PostgreSQL
- Why PostgreSQL Turns Names Lowercase, and What Double Quotes Do
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments