Время создания: 30.09.2017 21:54
Раздел: Postgres - syntaxis
Запись: xintrea/mytetra_db_mcold/master/base/1506797644tp57pdnv85/text.html на raw.githubusercontent.com
In this chapter, we will discuss how to delete the database in PostgreSQL. There are two options to delete a database −
Be careful before using this operation because deleting an existing database would result in loss of complete information stored in the database.
Using DROP DATABASE
This command drops a database. It removes the catalog entries for the database and deletes the directory containing the data. It can only be executed by the database owner. This command cannot be executed while you or anyone else is connected to the target database (connect to postgres or any other database to issue this command).
The syntax for DROP DATABASE is given below −
DROP DATABASE [ IF EXISTS ] name
The table lists the parameters with their descriptions.
We cannot drop a database that has any open connections, including our own connection from psql or pgAdmin III. We must switch to another database or template1 if we want to delete the database we are currently connected to. Thus, it might be more convenient to use the program dropdb instead, which is a wrapper around this command.
The following is a simple example, which will delete testdb from your PostgreSQL schema −
postgres=# DROP DATABASE testdb;
Using dropdb Command
PostgresSQL command line executable dropdb is a command-line wrapper around the SQL command DROP DATABASE. There is no effective difference between dropping databases via this utility and via other methods for accessing the server. dropdb destroys an existing PostgreSQL database. The user, who executes this command must be a database super user or the owner of the database.
The syntax for dropdb is as shown below −
dropdb [option...] dbname
The following table lists the parameters with their descriptions
The following table lists the command-line arguments dropdb accepts −
The following example demonstrates deleting a database from OS command prompt −
dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
The above command drops the database testdb. Here, I have used the postgres (found under the pg_roles of template1) username to drop the database.