Easy Tutorial
❮ Postgresql Drop Database Windows Install Postgresql ❯

PostgreSQL Selecting a Database

In the previous chapter, we discussed how to create a database. Now, let's talk about how to select the database we've 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     | 
 tutorialprodb  | 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 + database name to enter the database:

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

System Command Line Window

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

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

pgAdmin Tool

The pgAdmin tool is simpler; just click on the database to select it, and you can also view additional information about the database:

❮ Postgresql Drop Database Windows Install Postgresql ❯