How to select/access a database in PostgreSQL

Once you have created a PostgreSQL database, the next step is to select or access that database. A PostgreSQL database can be selected or accessed using multiple methods such as command prompt, pgAdmin, or SQL SHELL(psql). So, let’s learn how to access a Postgres database.

How to access/select a PostgreSQL database using SQL SHELL?

We have already created some databases, and now we will select one of them using the SQL SHELL(psql). If you have not yet created a PostgreSQL database, we recommend you to read our tutorial on database creation.

Step 1: Open psql

Firstly, type “psql” in the Windows search bar and open it:

image.png

Step 2: List of databases

Type “\l” to check all the available databases:

image.png

Step 3: Select database

Choose a desired database from the available databases, and type the following command to access it:

\c exampledb;

Here, the “\c” is a command used to select/access a database while “exampledb” is a database to be accessed:

image.png

This is how you can access any database using “psql”.

How to access/select a PostgreSQL database using Command prompt?

Follow the below-listed steps to select the desired PostgresQL database using the Windows command prompt(cmd):

Step1: Open cmd

Type “cmd” in the Windows search bar and click on the command prompt app to open it:

image.png

Step 2: Access Postgres

To access the Postgres bin directory, provide the complete path of the directory where Postgres is installed:

image.png

Now, you are successfully entered into the PostgreSQL bin’s directory.

Step 3: Select database

Type the below-given command to access the desired database:

psql -h localhost -p 5432 -U postgres exampledb

- Here, in the above snippet, we utilized the “-h” option/argument to specify the host, “-p” argument to specify the port number, and “-U” to specify the user name. While “exampledb” is the database to be accessed.

- Type the command, press the enter button, and provide the superuser password.

- Consequently, the above-mentioned command will select the database named “exampledb”, owned by the user “Postgres”,available on the “localhost”, and has a default port number “5432”:

image.png

The above snippet clarified that we have successfully accessed the “exampledb” database using the command prompt.

How to access/select a PostgreSQL database using pgAdmin?

The below-listed steps will guide you to select a Postgres database using pgAdmin:

Step 1: Open pgAdmin

Search “pgAdmin” in the windows search bar and open it:

image.png

Step 2: Select Database

Select the desired database from the available databases:

image.png

Step 3: Select Query Tool

Click on the “Tools” tab, and select the “Query tool” from the drop-down menu:

image.png

Clicking on the “Query Tool” will open a new window with an established connection to the desired database:

image.png

The above window shows that the desired database, i.e. “exampledb”, has been selected successfully.

Conclusion

There are multiple ways to select or access a PostgreSQL database, such as Command Prompt (cmd), SQL SHELL (psql), and pgAdmin 4. Type “\c” followed by a database name to select a database using psql. Type “psql hostname port number username databasename” to select a database using cmd. Open pgAdmin > select desired database> and open the “Query Tool” to select a database using pgAdmin. This write-up considered multiple methods along with screenshots to select/access a PostgreSQL database.