psql is the venerable command-line interactive tool that comes bundled with
PostgreSQL, that lets you connect to Postgres servers and run SQL queries and
Read on for some tips and tricks to make your psql sessions a bit more productive.
Print Time Taken for Queries
You can ask psql to print the time taken to execute every command or query,
Represent NULL Visibly
By default, it is impossible to distinguish between a NULL and an empty string
in psql’s output. Use the
\pset null str to use “str” wherever a NULL is
Repeatedly Execute a Query
You can repeatedly execute a query every N seconds, using the
\watch N command.
It will execute the last run query repeatedly.
Bored with plain old tables in psql query results? Try this:
Save Query Results as CSV
Save the results of any query as a CSV to a local file with the
Built-in Syntax Reference
psql comes with a built in quick syntax reference for all SQL commands. Use the
\h command to access it. To see the syntax for say “create table”, use
\h create table.
Edit Command in Editor
\e command to edit the last command you executed in your
You can edit the command, save it, and exit the editor to have psql execute
- Readline Support: Use Ctrl+R to recall commands, up/down arrows to scroll through history and Tab for completion of keywords, just like in bash.
- Command History: View the command history with the
\scommand. You can also save the entire list to a file with
- Save Your Customizations: You can see your funky borders everytime, by
creating a file called
$HOME. psql will read the contents of this file and execute them as psql commands before presenting the interactive prompt.
- Internal Queries: psql internally runs SQL queries to fetch the output
\dgetc. To see these queries, use
- Change Password: Change your PostgreSQL user password using the
\passwordcommand. If you’re a superuser, change any user’s password with
pgDash is a modern, in-depth monitoring solution designed specifically for PostgreSQL deployments. pgDash shows you information and metrics about every aspect of your PostgreSQL database server, collected using the open-source tool pgmetrics.
pgDash provides core reporting and visualization functionality, including collecting and displaying PostgreSQL information and providing time-series graphs, detailed reports, alerting, teams and more.