Updated on 2024-06-03 GMT+08:00

DROP DATABASE

Function

Deletes a database.

Precautions

  • Only the database owner or a user granted with the DROP permission can run the DROP DATABASE command. The system administrator has this permission by default.
  • The preinstalled POSTGRES, TEMPLATE0, and TEMPLATE1 databases are protected and therefore cannot be deleted. To check databases in the current service, run the gsql statement \l.
  • If any users are connected to the database, the database cannot be deleted. You can view the DV_SESSIONS view to check the database connections.
  • DROP DATABASE cannot be executed within a transaction block.
  • Before deleting a database, run the CLEAN CONNECTION TO ALL FORCE FOR DATABASE XXXX command to forcibly stop the existing user connections and backend threads, preventing database deletion failures caused by running backend threads. Forcibly stopping backend threads may cause data inconsistency in the current database. Therefore, execute this command only when you are sure to delete the database.
  • If DROP DATABASE fails to be run and the transaction is rolled back, run DROP DATABASE IF EXISTS again.

DROP DATABASE cannot be undone.

Syntax

1
DROP DATABASE [ IF EXISTS ] database_name;

Parameters

  • IF EXISTS

    Reports a notice instead of an error if the specified database does not exist.

  • database_name

    Specifies the name of the database to be deleted.

    Value range: an existing database name

Examples

See Examples in section "CREATE DATABASE."

Helpful Links

CREATE DATABASE

Suggestions

  • DROP DATABASE

    Do not delete databases during transactions.