psql Installation and Usage Documentation

Aug 5, 2025
1
0
1
What is Install psql?
psql is the command-line interface (CLI) for PostgreSQL, one of the most popular open-source relational database management systems (RDBMS) in the world.

It allows developers, system administrators, and database engineers to interact with PostgreSQL directly using SQL and various PostgreSQL commands.

Key Features of Install psql
Feature Description
SQL Query Execution You can write and execute SQL queries directly in the terminal.
Database Interaction Connect to PostgreSQL databases, view tables, create or drop databases, etc.
Meta-Commands psql has its own set of commands prefixed with a backslash (\) for managing databases (\dt, \l, \c, etc.).
Remote Connection Support Connect to remote PostgreSQL servers using host, port, and SSL options.
Scripting Support Run SQL scripts from files using -f or via redirection (psql < script.sql).
History & Autocomplete Maintains a command history and supports autocompletion for table names, commands, etc.
Export Options Export query results to files (CSV, tab-delimited, etc.).

Components Installed with psql (when installing PostgreSQL client)
psql: The CLI tool itself.

pg_isready: Utility to check server readiness.

pg_dump: Tool to export a PostgreSQL database (backup).

pg_restore: Restores backups created by pg_dump.

createdb, dropdb: Create or delete databases from the CLI.

createuser, dropuser: Create or delete database users.

These are part of the PostgreSQL client utilities, separate from the database server.

Detailed Installation by OS
Ubuntu / Debian
Command:
bash
sudo apt update
sudo apt install postgresql-client
Explanation:
postgresql-client installs just the command-line tools, not the actual PostgreSQL server.

You can connect to a PostgreSQL server (local or remote) using psql.

Useful if you don’t need to host a database, just connect to one.

macOS (with Homebrew)
🔧 Command:
bash
Copy
Edit
brew install libpq
brew link --force libpq
📝 Explanation:
Homebrew separates PostgreSQL tools into libpq.

You must link it to make tools like psql available globally.

Alternatively, update your shell’s PATH.

🪟 Windows (Using PostgreSQL Installer)
🔧 Steps:
Download the installer from https://www.postgresql.org/download/windows/

Run the installer.

During installation, select Command Line Tools.

After installation, optionally:

Add C:\Program Files\PostgreSQL\<version>\bin to your PATH.

Open Command Prompt or PowerShell, and run psql.

Explanation:
The installer includes both the server and client tools.

You can choose to install only what you need (e.g., skip pgAdmin or StackBuilder if unnecessary).

Common psql Commands
Here are some useful psql meta-commands:

Command Description
\l or \list List all databases
\c dbname Connect to a specific database
\dt List tables in the current database
\d tablename Show table structure
\du List roles/users
\q Quit psql

🧪 Example Use Cases
✅ Connecting to a local database:
psql -U postgres -d mydb
✅ Connecting to a remote server:
psql -h 192.168.1.100 -U dbuser -d mydb -p 5432
✅ Executing SQL from a file:
psql -U postgres -d mydb -f script.sql
✅ Exporting data from a query:
psql -U postgres -d mydb -c "SELECT * FROM users" > users.txt
🛠 When to Use psql
Development: Run and test SQL queries quickly.

Admin tasks: Manage roles, backup/restore databases.

Scripting: Automate tasks in shell scripts.

Debugging: View logs, run ad-hoc queries for analysis.