When working with PostgreSQL, you don't need to install the full database server just to connect to a remote instance. The PostgreSQL client tools package contains all necessary utilities including psql
, the command-line interface for PostgreSQL.
On Linux (Debian/Ubuntu)
For Debian-based systems, install the client package with:
sudo apt-get update
sudo apt-get install postgresql-client
On Linux (RHEL/CentOS)
sudo yum install postgresql
On macOS (using Homebrew)
brew install libpq
brew link --force libpq
On Windows
Download the interactive installer from postgresql.org and select only the "Command Line Tools" component during installation.
After installation, verify psql
is available:
psql --version
Use the following command to connect from your client machine (Box B) to the server (Box A):
psql -h [server_ip] -p [port] -U [username] -d [database_name]
Parameter | Description |
---|---|
-h | Server hostname or IP address |
-p | Port number (default is 5432) |
-U | Username for authentication |
-d | Database name to connect to |
If you encounter connection problems:
- Verify the server allows remote connections (
pg_hba.conf
) - Check firewall settings on both client and server
- Ensure the PostgreSQL service is running on the server
For GUI alternatives to psql
, consider:
- pgAdmin (official PostgreSQL GUI)
- DBeaver (multi-database GUI tool)
- TablePlus (modern database client)
Instead of typing parameters each time, set these environment variables:
export PGHOST=[server_ip]
export PGPORT=[port]
export PGUSER=[username]
export PGPASSWORD=[password]
export PGDATABASE=[database_name]
Now you can simply run psql
without any parameters.
Here's an example of running SQL commands from a script file:
psql -h dbserver.example.com -U admin -f query.sql
Where query.sql
contains:
-- query.sql
SELECT * FROM users WHERE active = true;
UPDATE statistics SET last_updated = NOW();
When working with PostgreSQL, the server (often called postgres) runs on your database host (Box A in this scenario), while client applications like psql can connect from remote machines (Box B). The key is that you only need the client utilities on Box B to connect to your remote PostgreSQL server.
The PostgreSQL client package contains essential tools including:
- psql: The interactive terminal
- pg_dump/pg_restore: For database backups
- createdb/dropdb: Database management utilities
For Debian-based systems:
sudo apt update
sudo apt install postgresql-client-15
For RedHat-based systems:
sudo yum install postgresql
Download the client-only installer from the official PostgreSQL site:
https://www.postgresql.org/download/windows/
Select only "Command Line Tools" during installation
Using Homebrew:
brew install libpq
brew link --force libpq
Once installed, connect to your remote server:
psql -h box-a.example.com -U username -d database_name
After connecting, run basic commands to verify:
\conninfo -- Shows connection information
\dt -- Lists tables
SELECT version(); -- Shows PostgreSQL version
For production environments, consider setting up pgpass for secure authentication:
# ~/.pgpass format
hostname:port:database:username:password
Common problems and solutions:
- Verify pg_hba.conf on the server allows remote connections
- Check if the PostgreSQL port (default 5432) is open
- Ensure the user has login privileges
Other options for connecting to PostgreSQL:
- pgAdmin: GUI administration tool
- DBeaver: Universal database tool
- VS Code extensions: PostgreSQL for VSCode