Forums

Setting Postgres config options programmatically

Hi, I need to temporarily change a Postgres client connection parameter at the start of a Python script, and revert to the default once the script has run. On local I can do this with

import subprocess

subprocess.run(["psql", "SET session_replication_role = 'replica';"])

# do stuff

subprocess.run(["psql", "SET session_replication_role = 'origin';"])

but psql is only accessible via the Databases tab on PythonAnywhere, and not directly. Is there some other way to do this? Thanks.

Hi, psql command is available in the console, you need to provide --host=HOSTNAME --port=PORT --username=super options, similarily as to pg_dump, see: https://help.pythonanywhere.com/pages/RegularPostgresBackups/.