PostgreSQL | Database | Drop a Database (DROP DATABASE)
This page explains how to delete a created database by using the DROP DATABASE command.
Drop a Database
To delete a created database, use the DROP DATABASE command. The syntax is as follows.
DROP DATABASE [ IF EXISTS ] name
Specify the database name (name) to delete that database.
To delete a database, the role executing the command must be a superuser or the database owner. Also, the database cannot be removed while the role executing the command or another role is connected to the database being deleted. You must connect to a different database before removing it. For how to connect to another database, see “Connect to a Specified Database”.
–
Now try it in practice. Delete the created devkuma2 database. Run the following.
postgres=# drop database devkuma2;
DROP DATABASE
postgres=#
The database has been deleted.
Then display the database list to confirm.
postgres=# \l
데이터베이스 목록
이름 | 소유주 | 인코딩 | Collate | Ctype | 액세스 권한
-----------+----------+--------+------------------+------------------+-----------------------
devkuma | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
postgres | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
rabbit | orange | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
sample | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
template0 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 | =c/postgres +
| | | | | postgres=CTc/postgres
(6개 행)
postgres=#
You can confirm that the devkuma2 database just deleted is no longer shown in the database list.
–
Next, check what happens when you try to delete the currently connected database. Connect to the devkuma database.
postgres=# \c devkuma
접속정보: 데이터베이스="devkuma", 사용자="postgres".
devkuma=#
While connected to the devkuma database, try deleting the devkuma database.
devkuma=# drop database devkuma;
오류: 현재 열려 있는 데이터베이스는 삭제할 수 없습니다
devkuma=#
The error “오류: 현재 열려 있는 데이터베이스는 삭제할 수 없습니다” is displayed, and deleting the database fails.
As shown here, you cannot delete the database to which you are currently connected.
–
This page explained how to delete a database by using the DROP DATABASE command.