Database Reference
In-Depth Information
Timing Executions
You may find it instructive to have psql output the time it took for each query to execute.
Use the
\timing
command to toggle it on and off.
When that is enabled, each query you run will report the duration at the end. For ex‐
ample, with
\timing on
, executing
SELECT COUNT(*) FROM pg_tables;
outputs:
count
--------
73
(1 row)
Time: 18.650 ms
Autocommit Commands
By default,
AUTOCOMMIT
is on, meaning any SQL command you issue that changes data
will immediately commit. Each command is its own transaction and is irreversible. If
you are running a large batch of precarious updates, you may want a safety net. Start by
turning off autocommit:
\set AUTOCOMMIT off
. Now, you have the option to roll back
your statements:
UPDATE
census
.
facts
SET
short_name
=
'This is a mistake.'
;
To undo the update, run:
ROLLBACK
;
To make the update permanent, run
COMMIT
;
Don't forget to commit your changes; otherwise, they'll automati‐
cally roll back if you exit psql.
Shortcuts
You can use the
\set
command to create useful typing shortcuts. Store universally
applicable shortcuts in your
psqlrc
file. For example, if you use
EXPLAIN ANALYZE VER
BOSE
once every 10 minutes and you're tired of typing it all out each time, set a variable:
\set eav 'EXPLAIN ANALYZE VERBOSE'
Now, whenever you want to enter
EXPLAIN ANALYZE VERBOSE
, simply type
:eav
(the
colon resolves the variable):
:eav SELECT COUNT(*) FROM pg_tables;