How to Connect to PostgreSQL from Terminal or GUI

Galaxy Glossary

How do I connect to a PostgreSQL database from the terminal or a GUI client?

Opens a PostgreSQL session by supplying host, port, user, and database details through the psql CLI or a graphical client.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

How to Connect to PostgreSQL from Terminal or GUI

Learn fast ways to open a PostgreSQL session from the command-line with psql or from popular GUI clients like pgAdmin, DataGrip, and Galaxy.

What are the quickest ways to connect?

Use psql for scripting or remote servers. Choose a GUI such as pgAdmin, DataGrip, or Galaxy when you want visual table browsing, query history, and autocomplete.

How do I connect from the terminal with psql?

Run a single command that passes host, port, user, and database. If the password is omitted, psql prompts securely. Environment variables like PGHOST and PGUSER shorten the command.

$ psql -h db.example.com -p 5432 -U analyst -d sales

Can I use a connection string instead?

Yes. Copy the URI from your cloud provider or compose it yourself. This works in both CLI and GUIs.

$ psql "postgresql://analyst:My$ecret@db.example.com:5432/sales?sslmode=require"

How do I connect with pgAdmin?

Open Servers → Register → Server. Fill Host, Port, Maintenance DB, Username, and Password. Click Save; the tool tests the connection immediately.

What about DataGrip or Galaxy?

Select New → PostgreSQL, paste your URI or fill individual fields, and press Test Connection. Accept SSL certificates if prompted, then click Connect.

How do I verify the session worked?

The prompt changes to databasename=> in psql. In a GUI, a green check appears and the schema tree expands. Run a quick query like SELECT NOW(); to confirm.

How can I automate logins securely?

Create a ~/.pgpass file or set environment variables in a CI pipeline. GUIs store encrypted credentials in system keychains, which you can disable for stricter policies.

Which SSL settings should I choose?

Cloud databases often require sslmode=require. Use verify-full for production to validate certificates, and disable only on trusted local networks.

Why How to Connect to PostgreSQL from Terminal or GUI is important

How to Connect to PostgreSQL from Terminal or GUI Example Usage


$ psql -h localhost -U postgres -d postgres -W
postgres=# SELECT version();

How to Connect to PostgreSQL from Terminal or GUI Syntax


CLI (psql):
psql [--host HOST] [--port PORT] [--username USER] [--dbname DB] [--password] [--set sslmode=MODE] [connection_string]

Connection string format:
postgresql://USER[:PASSWORD]@HOST[:PORT]/DBNAME[?param=value]

GUI fields:
- Host / Address
- Port (default 5432)
- Database (maintenance DB or target)
- Username
- Password
- SSL mode (disable, allow, prefer, require, verify-ca, verify-full)

Common Mistakes

Frequently Asked Questions (FAQs)

Do I need superuser rights to connect?

No. Any role with CONNECT privilege on the target database can log in.

Why does psql hang after I hit Enter?

It’s waiting for your password because PGPASSWORD is unset and .pgpass is missing.

Can I save multiple connections in Galaxy?

Yes. Galaxy Workspaces let you store and share named connections with granular access controls.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.