Good Place. Good Things. Good Time.

PostgreSQL Rename Database

PostgreSQL Rename Database

Introduction to PostgreSQL Rename Database

PostgreSQL renamed the database statement used to change the database name to another name; we can rename any database using the alter database statement. To rename the database in PostgreSQL, we first need to disconnect from the database from which we have connected; after disconnecting from the database, we need to connect to a different database. To rename any database in PostgreSQL, we must need the privileges of the owner of the database or need to admin of the database server. If we have to change the database name after database creation, we have changed or renamed the database name using the alter database statement.

Syntax

Below is the syntax of the renamed database in PostgreSQL.

Alter database old_database_name (Name of database which was we have change to different name.) RENAME TO new_database_name (New name of database which was we have changing.)

Below is the parameter description syntax of the renamed database

  • Alter database –We have using the alter database statement to rename the database with a different name. We can also use the alter database statement to change the feature of the database.
  • Old database name –This is defined as an old database name from the database server, which we have renamed to a different name.
  • Rename To –This keyword is defined as using this keyword we can change the database name with a different name in PostgreSQL. We have used rename to keyword with alter database statement in PostgreSQL.
  • New database name –This is defined as the new database name, which we have changed using the alter database command. This parameter is also important with the alter the database statement to change the database name.

How to Rename Database in PostgreSQL using various methods?

  • Below are the various methods which were used in PostgreSQL to rename the database in PostgreSQL.
  • We have renamed the database by using the alter database statement in PostgreSQL are as follows.
  • To rename the database using an alter database statement, we need to have superuser privileges or owner of the database privileges to rename the database.
  • The below example shows that we need to have superuser privileges or admin privileges to change the database name in PostgreSQL.

psql -U postgres -d postgres
alter database db_testing rename to db_test;
psql -U db_test -d postgres
alter database db_test rename to db_testing;

PostgreSQL Rename Database output 1

  • In the above first example, we have to change the name from Postgres user, Postgres user has privileges of a superuser; using superuser privileges, it is possible to change the name of the database.
  • In the second example, we have to change the name from db_test user; db_test user doesn’t have privileges to rename the database.
  • If we have to change the database name, we need to disconnect from the current database and connect to another database.
  • The below example shows that we need to change the database connection before renaming the database with a different name.

psql -U postgres -d postgres
c db_test
alter database db_test rename to db_testing;
c postgres
alter database db_test rename to db_testing;

PostgreSQL Rename Database output 2

  • In the above example, we have renamed the database by disconnecting the database with a different database.

To change the database name, we need to follow the below steps.

  • First, we need super user privileges or admin privileges to change the database name in PostgreSQL.
  • Disconnect from the database which name we have changing with a different name.
  • Disconnect all the session which was connected to the database, to check this session by using the pg_stat_activity catalog table.
  • Check all active sessions and terminate the same using the pg_terminate_backend command in PostgreSQL.

1. Rename database by using alter database statement –

  • Below is the example of rename a database using an alter database statement in PostgreSQL.
  • In the below example, we have to change the database name by using alter database statement in PostgreSQL.
  • In the below example, we have to change the database name from db_testing to db_test.
  • We have used Postgres user to change the database name in PostgreSQL.

l+
alter database db_testing rename to db_test;
l+

PostgreSQL Rename Database output 3

2. Alter the database using a command prompt

  • In the below example, we have to change the database name from the command prompt by using the alter database statement in PostgreSQL.
  • We have to change the database name by using Postgres user, Postgres user is the admin user of the database server.
  • In the above example, we have to change the database name from db_test to db_test_new by using the alter database statement.

output 4

3. Alter the database to change the name by disconnecting the sessions

  • In the below example, we have to change the database name by disconnecting the session from the database.
  • We have to change the database name from db_test_new to db_test are as follows.
  • We have to change the database name by using Postgres user after disconnecting all the session which was connected to db_test_new database.

select * from pg_stat_activity where datname="db_test_new";
alter database db_test_new rename to db_test;
l+

output 5

  • In the above example, we have checked the active sessions by using the pg_stat_activity catalog table.
  • Using this catalog table, we have checked the connected session of the db_test_new database. It will show the zero session is connected to the db_test_new database.
  • After successfully disconnecting all the sessions, we have renamed the database name as db_test_new to db_test in PostgreSQL.

Conclusion

We have renamed the database by using the alter database statement in PostgreSQL. We can rename the database using shell prompt as well as from database prompt. To rename the database in PostgreSQL, we need to have the privileges of the owner of the database or need to have super user privileges.

Recommended Articles

This is a guide to PostgreSQL Rename Database. Here we discuss How to Rename a Database in PostgreSQL using various methods. How to Rename Database in PostgreSQL using various methods

  1. PostgreSQL Wal
  2. Postgresql Count
  3. PostgreSQL Lock Table
  4. PostgreSQL Clustered Index

The post PostgreSQL Rename Database appeared first on EDUCBA.

Read original article here: PostgreSQL Rename Database

Read original article here: PostgreSQL Rename Database



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.