How to delete postgresql database on linux

16,174

Solution 1

Yes,

DROP DATABASE template1;

And dont forget to backup your database:

to backup: pg_dump name_of_database > name_of_backup_file.bak

to restore: psql empty_database < backup_file.bak

Solution 2

Make sure and end your SQL commands with a semicolon (;) Try issuing the command

DROP DATABASE template1;

with the semicolon on the end.

Share:
16,174
Scorb
Author by

Scorb

Updated on June 28, 2022

Comments

  • Scorb
    Scorb almost 2 years

    I am trying to learn postgresql on linux using the command line interface.

    I have added some databases a while back, following some tutorials (which I have since forgot everything I have learned).

    Now I want to delete these databases.

    I made the assumption that I should be doing this by using psql, the command-line interface to postgresql.

    You can see what I have tried in the following command line output, and that none of it has succeeded.

    psql (9.5.6)
    Type "help" for help.
    
    postgres=# \list
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
     template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
    (4 rows)
    
    postgres=# dropdb template1
    postgres-# \list
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
     template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
    (4 rows)
    
    postgres-# DROP DATABASE template1
    postgres-# \list
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
     template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
    (4 rows)