30 October 2026 · One command a day
How do I run a SQL query from the command line with psql?
The psql client can run one SQL query straight from the shell and then exit. You do not need to open an interactive session. Use it when you want a quick answer from a Postgres database, such as how many rows a table has, or when you want to put a query in a script or a cron job.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
sudo -u postgres psql -c "SELECT count(*) FROM users;" dbname
sudo -u postgres runs psql as the postgres system user. On Ubuntu, local connections use peer authentication, so your Linux user name has to match a Postgres role. The postgres role exists by default. If you have created a role for your own Linux user, you can leave out the sudo part. psql is the Postgres command-line client. -c tells it to run the quoted SQL and exit when it finishes. The double quotes pass the query as one argument, so the shell does not split it on spaces or treat * as a wildcard. dbname is the database to connect to, so replace it with your own. You can put any table name in place of users, and any query in place of this one.
What you will see
count
-------
1482
(1 row)
The first line is the column name. Below the divider is the value. The last line gives the number of rows returned. Here it is one, because count returns a single total. If you run the command from a directory the postgres user cannot read, such as /root, you may also see could not change directory to "/root": Permission denied. You can ignore that warning, because the query still runs.
When to use it
- After a deploy, count the rows in a table twice, a few minutes apart, to check that new sign-ups are still coming in.
- After a restore, compare row counts with the original server to confirm the restore worked.
- Write a small health check or cron job that records a number each day.
Watch out for
A query passed with -c runs straight away and is committed as soon as it succeeds. There is no confirmation prompt and no undo. That does not matter for a SELECT. An UPDATE or DELETE without a WHERE clause, though, changes every row in the table. Before you change anything, run a SELECT with the same WHERE clause to see how many rows it matches. If you are not sure about a change, take a backup first. Backing up a Postgres database with pg_dump takes one line.
Questions people ask
Why do I get "role root does not exist"?
By default, psql connects as the Postgres role with the same name as your Linux user, and there is no root role. Run psql as the postgres user with sudo -u postgres. Or log in with a password by adding -U appname -h 127.0.0.1, where appname is your database role.
How do I get just the number, with no header?
Add -t to drop the header and the row count, and -A to remove the padding. sudo -u postgres psql -tA -c "SELECT count(*) FROM users;" dbname prints only 1482, which is easy to use in a script.
Running queries against your own database from a shell is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.