AWS RDS
PostgreSQL
Database Management
Cloud Computing
Database Configuration

How can I change the Database Name in AWS RDS for Postgresql?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On Amazon RDS for PostgreSQL, changing a database name is a PostgreSQL operation, not an RDS console setting. The usual way is to run ALTER DATABASE ... RENAME TO ..., but you must do it from a different database, ensure no active connections remain on the target database, and then update any applications that still connect using the old name.

Use PostgreSQL's Rename Command

The SQL command itself is simple:

sql
ALTER DATABASE old_name RENAME TO new_name;

What makes the operation fail in practice is not the syntax, but the surrounding conditions. PostgreSQL will not rename a database while you are connected to that same database, and active sessions can block the rename too.

Connect to a Different Database First

If you want to rename appdb, connect to another database on the same RDS instance, such as postgres.

Example with psql:

bash
psql "host=mydb.abcdefghijkl.us-east-1.rds.amazonaws.com port=5432 dbname=postgres user=myadmin sslmode=require"

Then run the rename command from that session:

sql
ALTER DATABASE appdb RENAME TO appdb_prod;

That avoids the most common mistake, which is trying to rename the database you are currently using.

Remove Active Connections if Necessary

If other sessions are still connected to the database, PostgreSQL may refuse the rename. In that case, inspect active sessions:

sql
SELECT pid, usename, application_name
FROM pg_stat_activity
WHERE datname = 'appdb';

Then terminate them if you have planned the change window:

sql
1SELECT pg_terminate_backend(pid)
2FROM pg_stat_activity
3WHERE datname = 'appdb'
4  AND pid <> pg_backend_pid();

After the active sessions are gone, retry the rename.

What Changes After the Rename

The RDS instance endpoint does not change. What changes is the database name portion of the connection string.

Before:

text
postgresql://user:password@host:5432/appdb

After:

text
postgresql://user:password@host:5432/appdb_prod

That means the operational follow-up is just as important as the SQL command itself:

  • Update application configuration
  • Update migration jobs or admin scripts
  • Update monitoring or backup references that include the old name

Ownership and Permissions Still Apply

RDS does not remove normal PostgreSQL privilege rules. The role performing the rename must still have the authority to rename that database. In many setups, the master user or another privileged administrative role handles the change during a maintenance window.

Plan the Rename Carefully

Even though the command is small, it is still a disruptive change for connected applications. In production, treat it like a short maintenance operation:

  1. Stop or drain application traffic
  2. Terminate remaining sessions if necessary
  3. Rename the database
  4. Update clients to use the new name
  5. Validate application connectivity

That sequence is usually much safer than trying to rename a live, heavily used database while clients are reconnecting continuously.

Common Pitfalls

  • Trying to rename the database while connected to it is the most common failure mode.
  • Forgetting active sessions can make the command appear mysteriously blocked or rejected.
  • Assuming the RDS endpoint changes is incorrect; only the database name in the connection target changes.
  • Renaming the database without updating app configuration leaves the database change technically successful but operationally broken.

Summary

  • On RDS for PostgreSQL, rename the database with ALTER DATABASE old_name RENAME TO new_name;.
  • Connect to a different database, such as postgres, before you run the command.
  • Terminate active sessions on the target database if needed.
  • Update every client connection string that still points at the old database name.

Course illustration
Course illustration

All Rights Reserved.