Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start transaction automatically on psql login

I'm wondering if it's possible to have psql start a transaction automatically when I open a psql session on the command line. I know I can start a transaction manually using 'BEGIN;' but I'm wondering if that can be done automatically without me typing in 'BEGIN;' manually on the command line.

Thanks!

I did a google search but that didn't come up with any good results.

like image 573
Torsten Avatar asked Oct 17 '25 06:10

Torsten


1 Answers

You cannot have psql start a transaction when you login, but you can have it start a transaction with the first SQL statement you enter. For that, put a .psqlrc file into your home directory and give it the following content:

\set AUTOCOMMIT off

Note that that is a very bad idea (in my personal opinion). You are running the risk to inadvertently start a transaction that holds locks and blocks the progress of autovacuum. I have seen more than one PostgreSQL instance that suffered serious damage because of administrators who disabled autocommit in their interactive clients and kept transactions open. At the very least, add the following to your .psqlrc:

SET idle_in_transaction_session_timeout = '1min';
like image 91
Laurenz Albe Avatar answered Oct 20 '25 00:10

Laurenz Albe