Renaming a Role (User)
Renaming a PostgreSQL Role 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 Role
⛔️ When you connect to the PostgreSQL server, you’re likely accessing the database with the user you want to rename. However, PostgreSQL doesn’t allow renaming a user while you’re connected to it.
✅ The Fix: Create and Connect with a Different Role
To proceed, enter the following command:
CREATE ROLE <NEW_USER> SUPERUSER LOGIN PASSWORD '<USER_PASSWORD>';
Disconnect from the server with the \q
command then reconnect with the <NEW_USER> created:
docker exec -it <POSTGRESQL_CONTAINER_NAME> psql -U <NEW_USERNAME>
Step 3: Rename the Role
Now you can rename your role using the ALTER USER...RENAME TO
command:
ALTER USER <OLD_USER_NAME> RENAME TO <NEW_USER_NAME>;
Happy me! 🌱