EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Rename Database

PostgreSQL Rename Database

Updated May 24, 2023

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 need the privileges of the owner of the database or need 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 use 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 to alter the database statement to change the database name.

How to Rename a 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 using the alter database statement in PostgreSQL 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 example below shows that we need superuser 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 superuser privileges; 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; the 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 changed with a different name.
  • Disconnect all the sessions connected to the database to check this session 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 the database by using alter database statement –

  • Below is an example of renaming a database using an altered database statement in PostgreSQL.
  • In the below example, we have to change the database name by using an altered 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 users 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 must change the database name from db_test to db_test_new using the altered 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 as follows.
  • After disconnecting all the sessions connected to the “db_test_new” database, we need to change the database name using the Postgres user.
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 checked the active sessions 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 disconnecting all the sessions successfully, we renamed the database 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 the shell prompt and from the database prompt. To rename the database in PostgreSQL, we need to have the privileges of the database owner or super user privileges.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Rename Database” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Wal
  2. Postgresql Count
  3. Lock Table in PostgreSQL
  4. PostgreSQL Clustered Index
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test
 15+ Hour of HD Videos
5 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more