Skip to content
Gustavo Peixoto

Dec 6, 202616 min read

When people say "Postgres CLI" they mean psql, a free client with no API, no key, no per-unit price and no rate limit, which makes it easy to script into Activepieces flows or any other automation you already run.

Your limits come from the server, not the tool.

In the Tiger Data survey, 69.4% of respondents used psql, against 35% for pgAdmin, 26% for DBeaver and 13% for DataGrip.

Psql and Postgres CLI installation

Your limits come from the server, not the tool.

Those shares add up to well over 100%, so people mix tools, and psql is the one you'll find on every server and in every runbook.

Pick one install route per OS from the table, checked 2026-12-06. After any of them, psql --version should print psql (PostgreSQL) and a version number. Anything else means the install failed or the binary isn't on your PATH.

OS Command Confirm
macOS (Homebrew) brew install postgresql@17 psql --version
Debian/Ubuntu sudo apt install postgresql-client psql --version
RHEL/Fedora sudo dnf install postgresql psql --version
Windows Run the EDB installer, tick Command Line Tools psql --version in a new terminal

Which tools Postgres users reach for to query

Since the commands only install the client, details per platform follow.

Run the install, open a new shell, then check the version; Homebrew's postgresql@17 formula includes psql along with the server. If you get psql: command not found, Homebrew's bin directory isn't on your PATH.

On Debian and Ubuntu, postgresql-client installs psql without a server, which is what you want on a jump box or CI runner. On RHEL and Fedora, the postgresql package is the client.

A server needs a separate package, so you don't end up running one by accident.

Leave Command Line Tools ticked when the EDB installer asks which components to install, or there's no psql. Open a new terminal afterwards, because an old one has the old PATH.

This takes minutes, not a project: automate it in Activepieces free.

Connection and authentication options for psql

If you are looking for an API key, there isn't one: your credential is a Postgres role and its password, and every method below is just a different way of handing them over.

The table compares the six ways of supplying them, and the takeaway is that the easiest ones leak the most.

Method Ease of setup Security (password visible in history or ps?) Fit for scripts
Flags Easiest; nothing to create Host, user and database show in history and ps; psql has no password flag, so the password is prompted Poor for unattended runs, since the prompt blocks
URI Easy; one string Yes, if the password is embedded, in history and ps Fine if the string comes from a secret store
PG environment variables Easy Not in ps, but visible to anything that can read the process environment Good
~/.pgpass Moderate; one file with strict permissions No Good
pg_service.conf Moderate; named profiles No, if the password stays out of the file Good
SSL certificates Hardest; needs certificate issuance No password to leak Good once provisioned

Postgres connection strings and psql flags

Start with psql -h db.example.com -U app -d orders, which works immediately, and the error you meet first is FATAL: password authentication failed for user "app". That means the role or password is wrong, not that a key expired.

A failed two-step workflow in Activepieces showing a Google Calendar trigger connected to a Gmail send email action, with…

A URI such as postgresql://[email protected]/orders packs the same fields into one string, which is convenient for copying between tools. The mistake is pasting the password into it: that string lands in shell history and in the process list.

psql environment variables, .pgpass and service files

Set PGHOST, PGUSER, PGDATABASE and PGPASSWORD, and psql needs no arguments. That suits a CI job fed by a secret store, but the password sits in the environment of every child process.

A ~/.pgpass file avoids that: one line per host, port, database, user and password. psql ignores it with a warning unless permissions are 0600.

Name a connection in pg_service.conf and psql "service=prod" replaces five settings. Pair it with .pgpass for the password. For client certificates, set sslmode, sslcert and sslkey in the same place and the password disappears entirely.

Don't go looking for a free tier: psql is free software with no plan, quota or metering. The costs sit on the server side.

A managed Postgres host sets its own plans, prices, connection caps and network rules, so check its pricing page, not psql's documentation. Those caps, not the client, are what stop an unattended run.

Ten psql commands beginners use most

Start with these ten: \conninfo, \l, \c, \dt, \d, \du, \x, \timing, \i and \copy. The -c and -f flags, \q and ~/.psqlrc matter once nobody is watching the terminal.

The table shows each with a line you can paste, so you can check what you're connected to before anything runs.

Command Purpose Example
\conninfo Show the current connection \conninfo
\l List databases \l
\c Switch database \c mydb
\dt List tables \dt
\d Describe a table \d mytable
\du List roles \du
\x Toggle expanded output \x auto
\timing Show query duration \timing on
\i Run a file from inside psql \i script.sql
\copy Move data to or from a client-side file \copy mytable to 'out.csv' with (format csv, header)
-c Run one command and exit psql -c '\x' -c 'SELECT * FROM foo;'
-f Run a file and exit psql -f script.sql
\q Quit \q
~/.psqlrc Startup file, run on every session a line reading \timing on

Read the rows in three groups.

Run \conninfo first. Most "table does not exist" errors are the right query against the wrong database. \l lists databases, \c moves you, \dt lists tables, \d shows one table's columns. \du shows which role you're.

Use -c for a single statement and -f for a file. Both exit when done, which is what a scheduled job needs.

\i does the same from inside a session. \timing tells you which query is slow before a server-side limit tells you the hard way.

To move data without server file access, use \copy, which reads and writes files on your machine, not the server's. \x turns wide rows into one field per line, so they stop wrapping. Put \timing on, and anything else you always want, in ~/.psqlrc.

Common psql errors and how to fix them

Eight messages account for most psql failures, and each maps to one cause and one fix.

Message Cause Fix
Connection refused Server down, or wrong host or port Check -h and -p; confirm the server is running
role "x" does not exist -U missing, so psql uses your OS username Pass -U
password authentication failed Wrong password Set PGPASSWORD or use ~/.pgpass
no pg_hba.conf entry for host Server rejects your address or non-SSL connection Allow your IP; try sslmode=require
database "x" does not exist -d missing, so psql uses the username as database name Pass -d
sorry, too many clients already max_connections reached Pool or close connections
permission denied for table Role lacks a grant GRANT the privilege
canceling statement due to statement timeout Query ran past statement_timeout Fix the query or raise the timeout for the session

Look at the first five rows and most are setup mistakes, not outages, so the first alert you investigate is probably a misconfiguration. Most come from leaving out -h, -U or -d and letting psql guess.

Spell out all three and the message usually changes to the real one.

Server limits: max_connections and timeouts

On small managed tiers, connections are capped hard. Cloud SQL's f1-micro allows 25, RDS's t4g.micro 30 and Azure's B1ms 50, per Microsoft Learn. A few app instances plus a cron job can exhaust 25, and then too many clients hits whoever connects next.

Three rooms with full coat hooks and people holding spare coats

Smallest managed Postgres tiers allow few connections

Because Postgres has no rate limit, the fix is pooling, not backoff. Put a pooler such as PgBouncer in front, or cap your app's pool size well below the limit. For timeouts, SET statement_timeout = '30s'; stops a runaway query from holding a slot.

When you see permission denied, the role connected fine but lacks a grant, so run GRANT SELECT ON table TO role; as the owner. For garbled output, check \encoding and set it to match the database.

You can follow the rest of this with the builder open. Start free, no card.

Running psql in CI and scheduled jobs

Run psql -X -v ON_ERROR_STOP=1 -f job.sql and check the exit code, because without those flags psql keeps going after an error and the job looks green.

To keep a stray \set on the runner from changing behavior, -X skips ~/.psqlrc. ON_ERROR_STOP=1 makes psql quit at the first failed statement, so a half-applied migration stops instead of continuing. -c runs one command; -f runs a file.

  1. Set PGPASSWORD, or point to a .pgpass file, from the secret store.
  2. Run psql -X -v ON_ERROR_STOP=1 -f job.sql.
  3. Check the exit code.
  4. Fail the job on any non-zero code.
  5. Schedule it with cron or CI.

That order matters: the schedule comes last, after a manual run has failed on purpose once.

Behind PgBouncer, a connection pooler, its timeouts decide how a job dies. The defaults are a 15-second server connect, so a down database fails fast, and a 120-second query wait, so a job queued behind busy clients errors out after two minutes.

PgBouncer default timeouts for unattended jobs

Per PgBouncer GitHub, server idle is 600 seconds and server lifetime 3600, so a connection held longer than an hour is recycled. Cancel wait is 10 seconds, so a cancelled query can linger that long.

Never put the password in the connection string, because CI logs and ps output print it. Use PGPASSWORD injected from the secret store, or a .pgpass file with chmod 600, and keep set -x off around the call.

If the job moves into Activepieces, the same rule holds for the Postgres connection it stores: secret managers are part of its enterprise governance feature set, next to SCIM, custom RBAC, SSO/SAML and audit logs, so credentials are held centrally rather than in scripts or on laptops.

MoneyGram, Moneypenny, Alan and FundingSocieties run Activepieces in production. Your credentials are never ours to hold.

Give an AI coding agent its own role with SELECT on the tables it needs and nothing else. An MCP server wired to a superuser connection is a superuser with a chat box; the role is the boundary, and MCP doesn't enforce it for you.

If the agent should only read, make the database refuse writes rather than trusting the prompt.

Fix recurring Postgres jobs with Activepieces

When a nightly query has to land in Slack, Activepieces' PostgreSQL integration, one of the 751 integrations in its open source monorepo, replaces the cron-plus-psql script with a workflow you can see and edit. It doesn't replace psql for migrations or exploration.

When a script is enough and when a workflow is better

At 1am the failure you see is usually psql: error: connection to server at "db.internal" failed: FATAL: password authentication failed, and the cause is rarely the query.

It's a cron job that ran as a different user than the one whose ~/.pgpass you tested with, or a rotated password nobody updated on the box. The script was fine. The unattended environment around it wasn't.

Clock figure holding the wrong key at a locked door

When one person owns the job, it lives next to the database, and a failure that goes silent costs you nothing, a shell script is enough. Keep using psql for that.

When a workflow is the better fit

Once the job crosses into another app and someone other than you has to trust it, a workflow is better. Cron gives you no run history, no retry, and no place for a teammate to change the Slack channel without SSH access.

Activepieces workflow builder with a Google Calendar to Gmail automation showing a failed run due to authentication errors.

Activepieces puts the schedule, the query and the message on one canvas, with each run recorded. Flows are unlimited on every plan, including free, and a run is 1 credit however many steps it has, so this three-step report costs 1 credit a night.

The same report built both ways

In the illustration below, the same nightly report is built both ways. On top, a cron clock triggers a shell script that runs psql and posts to Slack; underneath, a schedule trigger, a PostgreSQL step and a Slack step are connected boxes.

Patched clock, script and bubble above three neat connected boxes

Read it as the same three jobs, trigger, query, notify, with the glue code replaced by connections you configure rather than maintain, which means changing system behavior doesn't require code changes.

Once you see the two side by side, the question is what the PostgreSQL step can actually do.

What the PostgreSQL integration can do

Instead of a crontab or environment file, the integration connects to a database with credentials you store once in a connection. It runs queries and passes the rows to the next step, so the Slack step can reference them directly.

It stays a client of the server, so server-side limits still apply. A role that's read-only in the database stays read-only here, which is why the previous section's advice carries over: give the workflow's connection a role that can't write.

Project Settings dialog showing Max Concurrent Jobs field set to Default (5) for the Secret Gadget Labs project.

It isn't psql. You won't use it for \d exploration, schema migrations or dump and restore. For those, stay in the terminal.

What to do with psql on Monday

Do six things in this order, and psql goes from a tool you type into to one you can leave running unattended. Each step removes a failure you would otherwise hit at the worst moment.

  1. Run psql --version. If it fails or the major version is older than your server's, fix that first, because an old client against a newer server misbehaves in ways that look like server bugs.
  2. Move credentials into ~/.pgpass and set its permissions to 0600. psql ignores the file if others can read it, so a loose mode looks exactly like a missing file, and you keep getting password prompts in jobs that have no one to answer them.
  3. Create a read-only role and use it for everything that only reports. A script with a typo in it can then only fail, never delete.
  4. Add a ~/.psqlrc with the settings you retype every session, such as \timing and \x auto. Keep it small, since it runs on every connection, including the ones inside scripts.
  5. Script one recurring query and run it with -v ON_ERROR_STOP=1. Without that flag psql prints the error, carries on to the next statement and exits with a success code, so cron and CI report a green run over a failed job.
  6. Decide which jobs go to a workflow tool. Anything that needs retries, a notification when it fails, or a hand-off to another system outgrows a shell script quickly. A query that runs on a schedule and writes to a file doesn't.

Frequently asked questions

Is psql the same thing as PostgreSQL? No. PostgreSQL is the database server; psql is the interactive command-line client that talks to it.

You can have psql on a laptop with no server at all, and a server running with no psql nearby, so check which one is missing before you debug the wrong thing.

Does psql need an API key or have rate limits? No to both. psql ships with PostgreSQL and is free, and it authenticates with the database role and password (or certificate) you give it, not a key.

Limits you hit come from the server or the managed host: connection caps, statement timeouts, and the free-tier ceilings of whichever provider you use.

How do I connect psql to a remote or cloud database?

Pass the host, port, user and database, or a single connection URI:

psql "postgresql://USER@HOST:5432/DBNAME?sslmode=require"

Most cloud providers require TLS, so a connection that hangs or is refused is usually a firewall or allowlist rule, or a missing sslmode.

Keep the password out of the command line and put it in PGPASSWORD or a ~/.pgpass file instead, so it stays out of your shell history.

Is there a GUI alternative to psql? Yes, several. pgAdmin is the graphical tool maintained alongside PostgreSQL, and general-purpose database editors also speak the Postgres protocol.

They help with browsing schemas and eyeballing results. They're a poor fit for scripts, CI jobs and cron, where psql's exit codes and non-interactive flags are what you need.

How do I exit psql or get out of a stuck prompt? Type \q and press Enter, or press Ctrl+D on an empty line, to leave psql.

If the prompt shows -# or '# instead of =#, psql is waiting for you to finish a statement or close a quote. Add the missing semicolon or quote, or press Ctrl+C to clear the buffer and start the line again.

Share

Build it

Set this up in minutes.

No code required. Connect your accounts, and Activepieces runs it from there.

Start free Talk to sales