Skip to content

Instantly share code, notes, and snippets.

@lovubuntu
Last active February 7, 2023 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lovubuntu/13a8647b4d238d347078 to your computer and use it in GitHub Desktop.
Save lovubuntu/13a8647b4d238d347078 to your computer and use it in GitHub Desktop.
History for psql commands

There's no history in the database itself, if you're using psql you can use "\s" to see your command history there.

You can get future queries or other types of operations into the log files by setting log_statement in the postgresql.conf file. What you probably want instead is log_min_duration_statement, which if you set it to 0 will log all queries and their durations in the logs. That can be helpful once your apps goes live, if you set that to a higher value you'll only see the long running queries which can be helpful for optimization (you can run EXPLAIN ANALYZE on the queries you find there to figure out why they're slow).

Another handy thing to know in this area is that if you run psql and tell it "\timing", it will show how long every statement after that takes. So if you have a sql file that looks like this:

\timing select 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment