52

I want to set a default statement_timeout for my access to a postgres database. After configuring my environment variables, I now have it where psql logs me on my preferred database and table. However, while I'm exploring several of tables in it, I'd like to have a statement timeout of around a minute. This can be done simply by typing SET statement_timeout TO '1min'; at the beginning of each session, but this is obnoxious to type every time. I don't have access to the server configuration nor would I want to change it. Ideally I could do something to the effect of alias psql='psql -c "SET statement_timeout TO '1min';"' except the-c` flag of psql doesn't allow interactive input. Are there any nice solutions to this problem, or am I always doomed to set the timeout manually for each interactive session?

3 Answers 3

85

You could use your .psqlrc file (if you don't have one in your home directory, create it; if you're on Windows instead of *nix, the file is %APPDATA%\postgresql\psqlrc.conf instead) and set the following command:

set statement_timeout to 60000; commit;

That setting is in milliseconds, so that'll set the timeout to 1 minute. .psqlrc isn't used with -c nor -X invocations of psql, so that should allow you to get your interactive-mode timeout to 1 minute.

You can then execute the following in psql to verify that the configuration has taken effect:

show statement_timeout;
Sign up to request clarification or add additional context in comments.

4 Comments

I didn't know about .psqlrc! On my version of psql (8.4.5) it doesn't count set [...] as a transaction, and so complained about commit with psql:~/.psqlrc:1: WARNING: there is no transaction in progress. Otherwise it worked. Thanks!
Sure, no problem. Ah yes, the wackiness of 8.4.x... I still have to use that version sometimes and am annoyed by what isn't in it yet or how it behaves differently. =)
This is certainly off topic by now, but 8.4.x frustration was my catalyst for setting up an ssh tunnel.
v11 (1) commit no longer needed. (2) Can now specific '1min'. HINT: Valid units for this parameter are "ms", "s", "min", "h", and "d".
59

Postgres allows you to set configuration parameters such as statement_timeout on a per-role (user) level.

ALTER ROLE <your-username> SET statement_timeout = '60s';

This change will apply to all new sessions for that user, starting on the next login.

Source: Postgres docs

2 Comments

this is by far the best solution
you saved my day. thank you. again this is by far the best solution.
5

If you're using a client that uses libpq under the hood (most of them except Java and .NET, I think), you can use the PGOPTIONS environment variable.

export PGOPTIONS="-c statement_timeout=7200000"

If you're using the URI-type connection string, you can add options to the options URI parameter in the same format, as long as they're properly uri encoded (percent encoding)

psql "postgres://user:pass@server-hostname/db_name?options=-c%20statement_timeout%3D3600000

When using keyword-value connection strings, you can specify options like this (single quotes required)

psql "host=server-hostname user=user dbname=db_name options='-c statement_timeout=3600000'"

Additionally, the Connection Service File (~/.pg_service.conf) may be useful and follows the same conventions as the above config. See https://www.postgresql.org/docs/current/libpq-pgservice.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.