In PostgreSQL transaction access mode can be changed to READ ONLY
(docs). In SQLAlchemy, isolation level can be changed for an engine, but no parameter is provided for read-only access mode (docs).
How to make an engine with READ ONLY
access mode on connections?
One solution is to execute a statement on every transaction:
engine = create_engine('postgresql+psycopg2://postgres:[email protected]:5432/')
@event.listens_for(engine, 'begin')
def receive_begin(conn):
conn.execute('SET TRANSACTION READ ONLY')
But it's better to set mode in BEGIN TRANSACTION
line an not a separate statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With