How to Connect to Postgres Database Server

When PostgreSQL is installed on a machine, some useful tools are also installed along with it, such as pgAdmin, psql, etc. These tools provide an interactive interface that assists us in working with Postgres in a better way. Using these tools, users can interact with the Postgres database server, manage database objects and its services, execute SQL queries, etc.

This post presents a detailed understanding of connecting to a Postgres database server via the following approaches:

  • Method 1: Using SQL Shell(psql)
  • Method 2: Using pgAdmin
  • Method 3: Using Command Prompt(CMD)

Let’s start with the psql.

Method 1: Using SQL Shell

Press the “win + S” button to launch the windows search bar > search the “psql” > and click on the “SQL Shell” app to open it:

img

If you didn’t change the default settings then there is no need to specify the server name, port number, database name, or user name; just hit the “ENTER” button. Provide the superuser password to connect to the Postgres database server:

img

The above snippet shows that we are successfully connected to the “postgres” database server. Execute any command/query of your choice to confirm if the connection is established or not:

\d
img

The “\d” command is executed to describe the available tables.

Method 2: Using pgAdmin

Press the “win + S” button to open the windows search menu > search the “pgadmin” > and click on the “pgAdmin” app to open it:

img

Clicking on the “pgAdmin” app will lead you to the following window:

img

Provide the superuser password and hit the “OK” button to connect to the Postgres Database Server.

Open the Query tool and use any command/query to confirm if the connection is established or not. To fulfill this task, expand the “SERVERS” tree > right-click on a particular database under the “Databases” tab > and select “Query Tool”:

img

In the query tool, execute any command of your choice, as shown below:

SELECT VERSION();
img

The stated command retrieves the currently installed Postgres version.

Method 3: Using Command Prompt(CMD)

In the windows search menu > search the “cmd” > and click on the “CMD” app to open it:

img

Once the CMD is opened, access the Postgres bin directory:

cd \Program Files\PostgreSQL\15\bin
img

Now execute the below-provided command to connect to the Postgres database Server:

psql -U postgres
img

The output shows that we are successfully connected to “postgres” database server via the CMD.

Conclusion

In Postgres, various tools, such as the “SQL Shell”, “pgAdmin”, and “Command Prompt” are used to connect to a Postgres database server. Launch the “SQL Shell” and specify the login details to connect to the Postgres database server via the “psql”. Open the “pgAdmin”, provide the superuser password, and hit the “OK” button to connect to the Postgres Database Server via pgAdmin. Using the CMD, you need to access the Postgres bin directory and then run the "psql -U postgres" command to connect to Postgres. This post explained how to connect to the Postgres database server using three different tools.