How to rename a PostgreSQL database
This article explains how to rename a PostgreSQL database.
Renaming a PostgreSQL database
You may need to rename a PostgreSQL database from time to time, such as when you migrate data from one account or hosting provider to another, or during site development.
The steps to rename a PostgreSQL database depend on whether or not your A2 Hosting account includes cPanel access.
Managed accounts with cPanel
If your A2 Hosting account includes cPanel access, you can use phpPgAdmin to rename a PostgreSQL database. You must export the database, create a new database, and then import the data from the old database into the new database.
To do this, follow these steps:
- Log in to cPanel.
- To export data from the existing database, follow these steps:
- In the Databases section of the cPanel home screen, click phpPgAdmin. The phpPgAdmin administration page appears in a new window.
- In the left pane of the phpPgAdmin window, expand Servers, expand PostgreSQL, and then click the name of the database that you want to export.
- On the top menu bar, click Export.
- In the Format column, click Structure and data.
- In the Options column, in the Format list box, select SQL.
- Under Options, click Download.
- In the file dialog, select a location and filename for the file on your local computer, and then click
To create a new database, follow these steps:
- In the Databases section of the cPanel home screen, click PostgreSQL Databases.
- Under Create New Database, type the name of the database in the Database Name text box.
- After the database is created, click
- Under Add User to Database, select the user that you want to add in the User list box.
- In the Database list box, select the new database.
To import the data into the new database, follow these steps:
The new database now contains all of the tables and data from the old database.
You can delete the old database if you want, but this is not required. In fact, you may want to keep it as an archive copy.
Semi-managed accounts without cPanel
If your A2 Hosting account does not include cPanel, you can quickly and easily rename a PostgreSQL database from the command line. To do this, follow these steps:
- Log in to your server using SSH.
- At the command prompt, type the following command to connect to PostgreSQL. Replace DBNAME with the name of a database that is not the database you want to rename, and replace USERNAME with a PostgreSQL username that has access privileges to the database:
psql DBNAME USERNAME
You cannot rename a database while you are currently connected to it.
To rename the database, type the following command. Replace OLD_DBNAME with the name of the database that you want to rename, and replace NEW_DBNAME with the database's new name:
ALTER DATABASE "OLD_DBNAME" RENAME TO "NEW_DBNAME"
To verify that the database was renamed, type the following command to list the databases: