4

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)
1

3 Answers 3

15

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

5

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

DROP DATABASE template1;

with the semicolon on the end.

1
  • and upper case DROP DATABASE too (my mistake)
    – perlyking
    Commented Jan 26, 2018 at 15:35
0

If your database contains 'template1' and 'template0' db name. You can use my script below:

  1. UPDATE pg_database SET datistemplate='false' WHERE datname='template1';
  2. DROP DATABASE template1;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.