From dd5ea8ca68f9f006b903e4b026172006cbaf8e18 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 28 Jun 2024 23:25:48 +0300 Subject: Add recommended Postgres connection options to reduce disconnections --- db.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'db.py') diff --git a/db.py b/db.py index eb7204b..f09a76a 100644 --- a/db.py +++ b/db.py @@ -33,7 +33,23 @@ class Db: database=os.getenv('DB_DATABASE', 'postgres'), port=os.getenv('DB_PORT', '5432'), user=os.getenv('DB_USER', 'postgres'), - password=os.environ['DB_PASSWORD']) + password=os.environ['DB_PASSWORD'], + + keepalives=1, + keepalives_idle=30, + keepalives_interval=10, + keepalives_count=5, + + application_name='diddle', + connect_timeout=10, + sslmode='prefer', + client_encoding='UTF8', + ) + + # Set the transaction isolation level + with self.conn.cursor() as cursor: + cursor.execute("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED") + self.conn.commit() def get_cursor(self): for _ in range(self.MAX_RETRIES): -- cgit v1.3