Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Connect to PostgreSQL Database using SQL Shell & pgAdmin

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.

SQL Shell (psql)

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

SQL Shell (psql)

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.

SQL Shell (psql)

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.

SQL Shell (psql)

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.

SQL Shell - Validate Version

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

SQL Shell List Command

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.

Reset Master Password in pgAdmin

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.

Reset Master Password in pgAdmin

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

pgAdmin

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

pgAdmin

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.

pgAdmin

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.

pgAdmin

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.

postgres Database in pgAdmin

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

pgAdmin Query Editor

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

Execute Queries in pgAdmin

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

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.