How can I time SQL-queries using psql?
Just turn on timing by entering:
From out bash, the "-c" option of `psql` does not allow this setting. You can do this with something like: ```psql --o /dev/null <
Timing can be turned on with
\timingat the psql prompt (as Caleb already said).
If you are on 8.4 or above, you can add an optional on/off argument to
\timing, which can be helpful if you want to be able to set timing on in .psqlrc - you can then set
\timing onexplicitly in a script where plain
\timingwould otherwise toggle it off
The time that
\timingreturns also includes the network latency, if you're connecting to a remote server.
When you don't want that and don't need the query output too, better use
EXPLAIN ANALYZE, which outputs the query plan with the planner estimates plus the actual execution times.
EXPLAIN ANALYZE SELECT foo from bar ;
Using `explain analyze` yields times that are approximately double what I see when using `\timing`, which is the opposite of what I would expect based on the comments here regarding network latency. I suspect that there is overhead in the normal execution of `analyze` that adds to the query time. Based on the docs, I think that `EXPLAIN (ANALYZE, TIMING OFF) SELECT foo FROM bar` will give you more useful timing information. See https://www.postgresql.org/docs/9.6/static/sql-explain.html for details.