PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - SELECT DATABASE



When there are multiple databases in the PostgreSQL Schema, then before starting any operation on a given database, that database need to be selected. The PostgreSQL \c command is used which closes the old connection and acquires a new one, using the specified database.

Syntax

The syntax of using \c command is given below:

\c DatabaseName;

To see the list of databases available in RDBMS, the following command can be used:

\l

OR

SELECT datname FROM pg_database;

Example: Select a database

To select a database with name testDB, the following statement can be used:

\c testDB;

To check all the available datasets, the following commands can be used:

\l
Result:
     Name    |  Owner   | Encoding |  Collate   |   Ctype    |      Access privileges      
-------------+----------+----------+------------+------------+-----------------------------
 SQLExample1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample2 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample3 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample4 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample5 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample6 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample7 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample8 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 testDB      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
(9 rows)


SELECT datname FROM pg_database;
Result:
   datname      
-------------
 SQLExample1 
 SQLExample2 
 SQLExample3 
 SQLExample4 
 SQLExample5 
 SQLExample6 
 SQLExample7 
 SQLExample8 
 testDB      
(9 rows)