All posts

How to Set Environment Variables for pgcli

pgcli is the beloved, fast, autocompleting PostgreSQL CLI tool used by engineers who can’t stand waiting. But running it without the right environment variables is like working blindfolded. Setting environment variables for pgcli the right way means faster logins, fewer typos, and no sensitive credentials left in history. Why Environment Variables Matter for pgcli pgcli connects to your database using your PostgreSQL credentials. Without environment variables, you might type pgcli -h myhost -

Free White Paper

End-to-End Encryption: The Complete Guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free. No spam. Unsubscribe anytime.

pgcli is the beloved, fast, autocompleting PostgreSQL CLI tool used by engineers who can’t stand waiting. But running it without the right environment variables is like working blindfolded. Setting environment variables for pgcli the right way means faster logins, fewer typos, and no sensitive credentials left in history.

Why Environment Variables Matter for pgcli

pgcli connects to your database using your PostgreSQL credentials. Without environment variables, you might type pgcli -h myhost -U myuser -d mydb again and again. That’s wasted time, and worse, it’s error-prone.

PostgreSQL supports environment variables that pgcli also respects. These include:

  • PGHOST – the host of your database
  • PGPORT – the port number (usually 5432)
  • PGDATABASE – the default database
  • PGUSER – your username
  • PGPASSWORD – your password

When these are set in your shell, you can connect with just:

pgcli

… and you’re in.

How to Set Environment Variables for pgcli

On Linux or macOS:

Continue reading? Get the full guide.

End-to-End Encryption: Architecture Patterns & Best Practices

Free. No spam. Unsubscribe anytime.
export PGHOST=db.example.com
export PGPORT=5432
export PGDATABASE=mydb
export PGUSER=myuser
export PGPASSWORD=mypassword

On Windows PowerShell:

$env:PGHOST="db.example.com"
$env:PGPORT="5432"
$env:PGDATABASE="mydb"
$env:PGUSER="myuser"
$env:PGPASSWORD="mypassword"

Save them in your shell configuration file (.bashrc, .zshrc, etc.) so they load every time you open a terminal.

Security and Best Practices

Never commit these variables to source control. If you share terminals or shells, make sure sensitive values like PGPASSWORD are cleared when not in use. Consider using a .env file with restricted permissions or a secrets manager. For production, avoid plain-text passwords and prefer more secure authentication methods like PGPASSFILE or SSL with certificates.

Troubleshooting Environment Variables in pgcli

If pgcli isn’t picking up your variables, check with:

env | grep PG

Ensure there are no hidden typos or extra spaces. Restart your terminal after making changes. Also confirm pgcli is pointing to the right PostgreSQL binary and drivers.

Environment Variables and Speed

With environment variables set, pgcli becomes an instant-launch tool. Autocomplete works faster because the connection is established without parsing manual flags each time. For those who jump between databases often, simply switch the variable values and reconnect.

When you remove repetition, you remove risk. Environment variables are the fastest way to make pgcli a seamless part of your daily workflow.

If you want to see this kind of convenience embedded into modern workflows—without spending days on setup—check out Hoop.dev. You can see it live in minutes.

Get started

See hoop.dev in action

One gateway for every database, container, and AI agent. Deploy in minutes.

Get a demoMore posts