Connecting PostgreSQL Using psql and pgAdmin

Installing the PostgreSQL on your machine will also install a couple of very handy tools, such as pgAdmin, and SQL SHELL. pgAdmin is a web-based GUI tool, while psql is a terminal-based tool. Both these tools assist us in working with Postgres.

This write-up will assist the beginners who have installed PostgreSQL on their PCs and are now looking to establish a connection to it. This post will cover the following topics:

  • Connecting to a Postgres Database Server Using psql.
  • Connecting to a Postgres Database Server Using pgAdmin.

So, what are you waiting for? Let’s start!

Connecting to a Postgres Database Server Using psql

SQL SHELL, or psql for short, is an open-source, terminal-based, cost-effective, secure, and scalable tool that allows us to execute queries/commands interactively.

Here are the stepwise instructions that will assist you in establishing a connection with the Postgres database server via psql:

Step #1: Launch the SQL SHELL

Firstly, you need to open the psql to avail any of its functionality. For this purpose, type psql in the Windows search bar and click on the respective app to open it:

img

Clicking on the targeted app will open the following window:

img

Step #2: Provide the Required Details

Now, provide all the required information, such as server address, database name, port number (default port number is 5432), user name, and password. Press enter if you did not change the default values during installation; as a result, SQL SHELL will pick the default values for each field. Finally, specify the password that you set during the PostgreSQL installation:

img

Step #3: Interact With the Database Server

Upon connecting to the Postgres Database Server, you can execute any statement/command. The below statement will show you the list of available databases:

img

Similarly, you can execute any statement of your choice depending on the situation. For instance, run the below-given command to see the Postgres version:

SELECT VERSION();
img

This way, you can issue any statement of your choice.

Connecting to a Postgres Database Server Using pgAdmin

Another way to connect to a PostgreSQL database server is by using pgAdmin. It is a management tool for Postgres that allows us to interact with the Postgres database server through an interactive user interface.

Let’s learn the step-by-step procedure to connect to a Postgres database using pgAdmin:

Step #1: Launch the pgAdmin

Firstly, type the pgAdmin in the Windows search bar and then click on the respective app to open it:

img

Clicking on the desired app will open the following window:

img

Provide the superuser password that you set during the Postgres installation, and then click on the OK button.

Step #2: Register Server

Now, right-click on the “servers” node, then select register > server:

img

Clicking on the server will open a new window:

img

Specify the server’s name and then click on the Connection tab to provide the hostname/address and super user password:

img

Clicking on the save button will register the server.

Step #3: Interact With the Database Server

Now, in order to interact with the database server, you need to open the query tool. To do this, right-click on the desired database and then the query tool:

img

Clicking on the query tool will lead you to the following window:

img

Here, you can specify any statement of your choice. For example, to check the Postgres version, you can execute the following command from the query tool:

SELECT VERSION();
img

That was all the required information that you need to learn in order to connect to a database server using psql or pgAdmin.

Conclusion

In PostgreSQL, we can connect to the database server using the SQL SHELL(psql) or pgAdmin. For instance, connecting to PostgreSQL using psql involves the following steps: launch psql > provide the required details like server address, port number, password, etc. > interact with Postgres by executing any query of your choice. In order to connect to Postgres using pgAdmin, you have to launch the pgAdmin, then create/register the database server by providing the required details and finally open the query tool to execute the statements/queries of your choice. This write-up explained how to connect to Postgres using psql and pgAdmin.