PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Administration / PostgreSQL Rename Database: A Quick Guide

PostgreSQL Rename Database: A Quick Guide

Summary: in this tutorial, you will learn step by step how to rename a PostgreSQL database even with the open connections.

PostgreSQL rename database steps

To rename a PostgreSQL database, you use the following steps:

  1. Disconnect from the database that you want to rename by connecting to a different database.
  2. Check and terminate all connections to the database that is being renamed.
  3. Use ALTER TABLE statement to rename the database to the new one.

Let’s take a look at an example of renaming a database to the new one.

Suppose the database that you want to rename is db. If the db database is not available in your database server, you can create it by using the CREATE DATABASE statement as follows:

1
CREATE DATABASE db;

To rename the db database to newdb database, you follow these steps:

First, disconnect from the database that you want to rename by connecting to another database e.g., postgres. If you use the psql command line, you can use the following command to connect to the postgres database:

1
db=# \connect postgres;

Next, check the all active connections to the db database by using the following query:

1
2
3
4
5
6
SELECT
    *
FROM
    pg_stat_activity
WHERE
    datname = 'db';

The query returned the following output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-[ RECORD 1 ]----+------------------------------
datid            | 35918
datname          | db
pid              | 6904
usesysid         | 10
usename          | postgres
application_name | psql
client_addr      | ::1
client_hostname  |
client_port      | 56412
backend_start    | 2017-02-21 08:25:05.083705+07
xact_start       |
query_start      |
state_change     | 2017-02-21 08:25:05.092168+07
waiting          | f
state            | idle
backend_xid      |
backend_xmin     |
query            |

As you can see, currently, there is one connection to the db database.

You may find that the database that you want to rename has many active connections. In this case, you need to inform the respective users as well as the application owners before terminating the connections to avoid data loss.

Then, terminate all the connections to the db database:

1
2
3
4
5
6
SELECT
    pg_terminate_backend (pid)
FROM
    pg_stat_activity
WHERE
    datname = 'db';

After that, rename the db database to newdb by using the ALTER TABLE RENAME TO statement as follows:

1
ALTER DATABASE db RENAME TO newdb;

Finally, if your database is being used by applications, you should modify the connection string.

In this tutorial, you have learned how to rename a PostgreSQL database to a new one by using the ALTER TABLE RENAME TO statement.

Previous Tutorial: 17 Practical psql Commands That You Don’t Want To Miss
Next Tutorial: How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.