PostgreSQL Drop Database

we have already created a database in our previous chapter. You can Drop database using either of the following methods:
  1. OS Command Prompt
  2. SQL Prompt
    1.OS Command Prompt:
    • dropdb -- remove a PostgreSQL database
    • dropdb destroys an existing PostgreSQL database. The user who executes this command must be a database superuser or the owner of the database.
    • dropdb is a 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.
    Synatax:
    dropdb [connection-option...] [option...] dbname
    dropdb accepts the following command-line arguments:

    Options
                                                                    Description
    dbname
    Specifies the name of the database to be removed.
    -e --echo
    Echo the commands that dropdb generates and sends to the server.
    -i --interactive
    Issues a verification prompt before doing anything destructive
    -V --version
    Print the dropdb version and exit.
    -? --help
    Show help about dropdb command line arguments, and exit.
    dropdb also accepts the following command-line arguments for connection parameters:
    -h --host
    Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.
    -p  --port
    Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.
    -U --username
    User name to connect as
    -w --no-password
    Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.
    -W
    --password
    Force dropdb to prompt for a password before connecting to a database.
    This option is never essential, since dropdb will automatically prompt for a password if the server demands password authentication. However, dropdb will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.

    Examples:

    Following example demonstrates deleting a database from OS command prompt:
    To destroy the database demo on the default database server:

    $ dropdb demo
    First we need to Check what database do you want drop:
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    ------------+----------+----------+-------------+-------------+-----------------------
     account    | u1       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     musicdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     sales      | u2       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
    (8 rows)
    
    To destroy the database demo using the server on host eden, port 5000, with verification and a peek at the underlying command:
    [postgres@r1 ~]$ dropdb -p 5432 -h r1 -i -e sales
    Database "sales" will be permanently removed.
    Are you sure? (y/n) Y
    Please answer "y" or "n".
    Are you sure? (y/n) y
    Password: 
    DROP DATABASE sales;
    dropdb: database removal failed: ERROR:  database "sales" is being accessed by other users
    DETAIL:  There is 1 other session using the database.
    [postgres@r1 ~]$ 
    
    solution:
    • Above statement throws error becouse  database "sales" is being accessed by other users We cannot drop a database that has any open connections/session, 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.
    • another way is we can kill the session using following command
    postgres=# SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'sales' AND pid <> pg_backend_pid();
     pg_terminate_backend 
    ----------------------
     t
    (1 row)
    after terminated above session you can drop the database:
    [root@r1 ~]# dropdb -p 5432 -h r1 -i -e sales
    bash: dropdb: command not found
    [root@r1 ~]# su - postgres
    [postgres@r1 ~]$ dropdb -p 5432 -h r1 -i -e sales
    Database "sales" will be permanently removed.
    Are you sure? (y/n) y
    Password: 
    DROP DATABASE sales;
    [postgres@r1 ~]$ 
    then check database is whether droped or not ollwing command:
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    ------------+----------+----------+-------------+-------------+-----------------------
     account    | u1       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     musicdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
    (7 rows)
    2.SQL Prompt:
    • DROP DATABASE -- remove a database
    • DROP DATABASE 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. Also, it cannot be executed while you or anyone else are connected to the target database. (Connect to postgres or any other database to issue this command.)
    • DROP DATABASE cannot be undone. Use it with care
    dropdb accepts the following command-line arguments:

    Options
                                                                    Description
    IF EXISTS
    Specifies the name of the database to be removed.
    -e --echo
    Echo the commands that dropdb generates and sends to the server.

    Notes:
    • DROP DATABASE cannot be executed inside a transaction block.
    • This command cannot be executed while connected to the target database. Thus, it might be more convenient to use the program dropdb instead, which is a wrapper around this command.
    Examples:
    First we will check the List of database:
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privile
    ges   
    ------------+----------+----------+-------------+-------------+-----------------
    ------
     account    | u1       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     musicdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
    (7 rows)
    
    Drop the "musicdb" database:
    postgres=# drop database musicdb;
    ERROR:  database "musicdb" is being accessed by other users
    DETAIL:  There is 1 other session using the database.
    
    postgres=# SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'musicdb' AND pid <> pg_backend_pid();
     pg_terminate_backend 
    ----------------------
     t
    (1 row)
    
    postgres=# drop database musicdb;                                               
    DROP DATABASE
    
    Then check database whether droped or not:
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    ------------+----------+----------+-------------+-------------+-----------------------
     account    | u1       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     temp0copy2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres     
         +
                |          |          |             |             | postgres=CTc/postgres
    (6 rows)

                                    

    No comments:

    Post a Comment