Connect to PostgreSQL Database using SQL Shell & pgAdmin (2024)

Here you will learn to connect to your PostgreSQL database with SQL Shell and pgAdmin tool.

Connect PostgreSQL Database using SQL Shell (psql)

SQL Shell is a command-line tool to connect and work with the PostgreSQL database. You can use it to create, alter, delete databases, tables, etc. in the PostgreSQL database.

Let's connect to the default postgres database using SQL Shell (psql). On Windows, press Windows keys -> All apps -> PostgreSQL 14 -> click on SQL Shell (psql), as shown below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (1)

This will launch SQL Shell (psql) command-line interface, as shown below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (2)

First, you need to enter the PostgreSQL server name. if it is on your localhost then press Enter. The default value mentioned in the square bracket e.g. [localhost] is the default server.

Next, it will ask for the Database to connect to the server. Press Enter to connect with the default database named postgres. If you want to connect to another existing database then enter that name and press Enter.

Next, enter the port number on your server to connect. This was the port you gave at the time of installation.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (3)

Press Enter to continue with the default port. Next, provide username and password to connect with the database.

The default username is postgres and enter a password that you gave during installation.

After providing the correct username and password, you will be connected to the postgres database, as shown below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (4)

Now, let's validate the version of the database using SELECT version(); command, as shown below. This will show the currently installed version of PostgreSQL.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (5)

To check what all databases are there, enter \l command in psql, as shown below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (6)

So, this is how you can connect to your PostgreSQL database with SQL Shell (psql) and start working.

SQL Shell can be opened from the command prompt. Open a command prompt and navigate to your local C:\Program Files\PostgreSQL\14\bin and enter the following command. (you may enter this path to your environment variable)

psql -h localhost -p 5432 -d postgres -U postgres

Here,

–h is host name
-p is port number
-d is database name
-U is for user name

It will prompt for the password, where you need to enter a password. It will connect you to postgres database as 'postgres' superuser.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (7)

Thus, you can connect to the PostgreSQL database using SQL Shell.

Connect to PostgreSQL using pgAdmin

The pgAdmin tool is installed as a part of PostgreSQL installation. It is GUI based tool that allows you to interact with PostgreSQL servers and databases.

On Windows 10, search for pgAdmin and click on it to open it. It first asks for the master password. You can click on the Reset Master Password button to set it if you don't have it.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (8)

Clicking on Reset Master Password will open 'Set Master Password' dialog, as shown below. Here you can provide your master password and click OK.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (9)

After setting the master password, the pgADmin will look like below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (10)

Expand the Servers node in the left pane. This will ask for the password for the default 'postgres' superuser which you set during installation time.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (11)

Enter the password for the user 'postgres' and click on OK button. This will expand the default server, which is PostgreSQL 14. It will show all the databases and Login/Group Roles in the left pane, as shown below.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (12)

As you can see above, it displays the default 'postgres' database. Click on it to see all the information about it on the right side pane.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (13)

Open the query tool by clicking on Tools -> Query Tool or clicking on the bellow icon.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (14)

Write queries in Query Editor and click on the Execute button to execute a query against the connected database.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (15)

This is how you can use SQL Shell command-line tool or pgAdmin GUI tool to work with PostgreSQL databases.

Connect to PostgreSQL Database using SQL Shell & pgAdmin (2024)
Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5696

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.