PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL DROP DATABASE

PostgreSQL DROP DATABASE

 Summary: in this tutorial, you will learn how to delete existing database by using PostgreSQL DROP DATABASE statement.

Introduction to PostgreSQL DROP DATABASE statement

Once a database is no longer needed, you can delete it by using the DROP DATABASE statement. The following illustrates the syntax of the DROP DATABASE statement:

1
DROP DATABASE [IF EXISTS] name;

To delete a database:

  • Specify the name of the database that you want to delete after the DROP DATABASE clause.
  • Use IF EXISTS to prevent an error from removing a non-existent database. PostgreSQL will issue a notice instead.

The DROP DATABASE statement deletes catalog entries and data directory permanently. This action cannot be undone so you have to use it with caution.

Only the database owner can execute the DROP DATABASE statement. In addition, you cannot execute the DROP DATABASE statement if there is any active connection to the database. You have to connect to another database e.g., postgresqlto execute the DROP DATABASE statement.

PostgreSQL also provides a utility program named dropdbthat allows you to remove a database. The dropdbprogram executes the DROP DATABASE statement behind the scenes.

Delete a database that has active connections

To delete the database that still has active connections, you can follow the steps below:

First, find the activities that are taken place against the target database, you can query the pg_stat_activityview as the following query:

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

Second, terminate the active connections by issuing the following query:

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

Notice that if you use PostgreSQL version 9.1 or earlier, use the procpidcolumn instead of the pidcolumn because PostgreSQL changed procidcolumn to pidcolumn since version 9.2

Third, execute the DROP DATABASE statement:

1
DROP DATABASE target_database;

PostgreSQL DROP DATABASE examples

We will use the databases created in the PostgreSQL create database tutorial for the demonstration. If you don’t have these databases available, you can create them by executing the following statements:

1
2
CREATE DATABASE hrdb;
CREATE DATABASE testdb1;

Delete database that has no active connection example

To remove the hrdbdatabase, use the hrdb owner to connect to a database other than hrdbdatabase e.g., postgres and issue the following statement:

1
DROP DATABASE hrdb;

PostgreSQL deleted the hrdbdatabase.

Delete database that has active connections example

The following statement deletes the testdb1database:

1
DROP DATABASE testdb1;

However, PostgreSQL issued an error as follows:

1
2
3
ERROR: database "testdb1" is being accessed by other users
SQL state: 55006
Detail: There is 1 other session using the database.

To delete the testdb1 database, you need to follow the steps as described in the above section.

First, query the pg_stat_activityview to find what activities are taking place against the testdb1database:

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

PostgreSQL DROP DATABASE - testdb1 activities

The testdb1database has 1 connection from localhosttherefore it is safe to terminate this connection and remove the database.

Second, terminate the connection to the testdb1database by using the following statement:

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

Third, issue the DROP DATABASE command to remove the testdb1database:

1
DROP DATABASE testdb1;

PostgreSQL deleted the testdb1permanently.

In this tutorial, you have learned how to use the PostgreSQL DROP DATABASE statement to delete a database. In addition, you also learned how to delete a database that has active connections.

Previous Tutorial: PostgreSQL ALTER DATABASE
Next Tutorial: PostgreSQL Array

PostgreSQL Quick Start

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

Databases Management

  • Create New Databases
  • Modify Databases
  • Delete Databases
  • Copy a Database
  • Get Database Object Sizes

PostgreSQL Roles Administration

  • Introduction to PostgresQL Roles

PostgreSQL Backup & Restore

  • PostgreSQL Backup Databases
  • PostgreSQL Restore Databases

Tablespaces Management

  • Creating Tablespaces
  • Changing Tablespaces
  • Deleting Tablespaces

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

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

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.