English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Select a Database in PostgreSQL

In the previous section, we discussed how to create a database, and now we will discuss how to select the database we created.

Database command window

In the PostgreSQL command window, we can enter SQL statements after the command prompt:

postgres=#

Use \l to view existing databases:

postgres=# \l
                             List of databases
   Name        |        Owner        | Encoding | Collate | Ctype        |        Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres | postgres | UTF8     | C        | C        | 
 w3codeboxdb | postgres | UTF8     | C        | C        | 
 template0 | postgres | UTF8     | C        | C        | =c/postgres          +
           |        |        |        |        |    postgres=CTc/postgres
 template1 | postgres | UTF8     | C        | C        | =c/postgres          +
           |        |        |        |        |    postgres=CTc/postgres
(4 rows)

Next, we can use \c + Enter the database name to access the database:

postgres=# \c w3codeboxdb
You are now connected to the database "w3codeboxdb" as user "postgres".
w3codeboxdb=#

System command line window

In the system command line, we can add the database name after connecting to the database to select the database:

$ psql -h localhost -p 5432 -U postgress w3codeboxdb
Password for user postgress: ****
psql (11.3)
Type "help" for help.
You are now connected to the database "w3codeboxdb" as user "postgres".
w3codeboxdb=#

pgAdmin tool

The pgAdmin tool is simpler, just click on the database selection directly, and you can also view some additional information about the database: