Skip to main content

Renaming a Database

Renaming a PostgreSQL Database Inside a Docker Container: Step-by-Step Guide

Step 1: Connect to the PostgreSQL Server

To start, connect to the PostgreSQL server within the Docker container by running the following command:

docker exec -it <POSTGRESQL_CONTAINER_NAME> psql -U <POSTGRESQL_USERNAME>

Step 2: Switch to a Different Database

⛔️ When you connect to the PostgreSQL server, you’re likely accessing the database you want to rename. However, PostgreSQL doesn’t allow renaming a database while you’re connected to it.

✅ The Fix: Connect to a Different Database

To proceed, list all available databases on the server, then connect to another one:

\l
\c <ANOTHER_DB_NAME>

Step 3: Rename the Database

Now you can rename your database using the ALTER DATABASE...RENAME TO command:

ALTER DATABASE <OLD_DB_NAME> RENAME TO <NEW_DB_NAME>;

Happy me! 🌱