EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Rename Database

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » 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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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

EDUCBA

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

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

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More