PostgreSQL – How to Change Database Name Without Interruption

postgresqlpostgresql-11

I have a production database that I'm told is "named incorrectly". Is there a way to change the name without interruption?

The database in question is named something like prod_api_db and should be named api_db.

I was hoping there would be a way to create an "alias" so to speak, such that both names are valid and point to the same database for a time period while all of the app configurations are updated with the new name. Once everything's updated, the old name could be removed.

 -- Stage 1, create alias "api_db":

apps ==> prod_api_db
                    > actual_database
              api_db

-- Stage 2, Update app configs:

    prod_api_db
               > actual_database
apps ==> api_db

-- Stage 3, Remove old name:

apps ==> api_db > actual_database

Does that sort of functionality exist in postgres?

Just for background, this is postgres 11. I do have dev, qa, and production servers to work with, so I can test before apply a change.

Pardon me if I'm being very ignorant about this. I'm not a DBA… just an SRE working for a small company who's been given charge of some database, and I haven't found a good answer yet.

Best Answer

No, a database can have only a single name.

You will have to disconnect all sessions before you can rename the database.

You will have to update the database name in all connection strings. The latter can be avoided if you use LDAP connection string lookup, but I guess you don't want to run an LDAP server in a small organization.

Related Question