Set PostgreSQL configuration parameter in SQLAlchemy
NickName:Horned Owl Ask DateTime:2014-10-07T22:21:21

Set PostgreSQL configuration parameter in SQLAlchemy

I'm using Pyramid and SQLAlchemy for my REST server. For the logging purposes I need some way how to determine user name in postgresql after update trigger function. One way is setting some runtime configuration parameter before update execution and using this value in trigger function.

In psql terminal it can be done using

set myapp.user_name = 'User Name';

This parameter will be subsequently used in postgresql trigger.

Is it possible to set this parameter in Pyramid / SQLAlchemy application? I suppose I can use SQLAlchemy Events. But I'm not sure which event is correct for this case.

Copyright Notice:Content Author:「Horned Owl」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26238256/set-postgresql-configuration-parameter-in-sqlalchemy

Answers
Sergey 2014-10-08T01:03:44

You can register a Pyramid event handler which would set the parameter on any new request:\n\nfrom pyramid import events\n\ndef on_new_request(event):\n \"\"\"\n you can access request as event.request \n and the current registry settings as event.request.registry.settings\n \"\"\"\n session = DBSession()\n session.execute(\"SET blah TO 'foo'\")\n\n\ndef app(global_config, **settings):\n\n config = Configurator(...)\n config.add_subscriber(on_new_request, events.NewRequest)\n\n\nSee Deployment Settings and events.NewRequest for more details.\n\nOne thing to watch out is to make sure you're always using the same session object - SQLAlchemy maintains a pool of connections and, if you commit your session, all subsequent operations will use a brand-new session which is not pre-configured with your settings. In other words, you should not do session.commit(), session.rollback(), transaction.commit() etc. in your code and instead rely on ZopeTransactionExtension committing/rolling back the transaction at the end of the request/response cycle. Which is a recommended practice anyway. ",


More about “Set PostgreSQL configuration parameter in SQLAlchemy” related questions

Set PostgreSQL configuration parameter in SQLAlchemy

I'm using Pyramid and SQLAlchemy for my REST server. For the logging purposes I need some way how to determine user name in postgresql after update trigger function. One way is setting some runtime

Show Detail

How to set a configuration parameter at DB level in PostgreSQL permanently?

I am aware of the below command: ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT } On server restart, will these be overwritten by the parameter set in postgresql.conf

Show Detail

Read configuration parameter in postgresql using JDBC

I have set a configuration parameter in postgresql 9.2.4 in the following way in Datastudio SET foo.test = 'foo.test' I can access that value in Datastudio using SELECT current_setting('foo.test...

Show Detail

SQLAlchemy async event listen engine.sync_engine SET string parameter replacement

I am trying to listen to the engine_connect event, grab the cursor on that connection, then issue a SET query, however I can't seem to get the parameter replacement to work. I ONLY have this issue ...

Show Detail

flask postgresql sqlalchemy configuration for production and development

I am trying to configure my postgresql for both production and development envronment in flask.Right now my configuration only works for local environment.I want to also make it work for productio...

Show Detail

How to find postgresql uri for SQLALCHEMY config

I am trying to connect my flask app to my prostgreSQL db, and i find this configuration example (below code). I just do not know how to find my postgreSQL URI app = Flask(__name__) #how do i know my

Show Detail

Specify `statement_timeout` in Postgresql with sqlalchemy?

The following statement_timeout option works on some Postgresql databases and on others, I get Unsupported startup parameter: options. Why? Is this possibly a difference between Postgres 9.4 and 9...

Show Detail

Connecting postgresql with sqlalchemy

I know this might be really a simple question but I don't know the solution. What is happening here when I try to connect to postgresql? I am self learner in this field of database and programming so

Show Detail

How to set a column default to a PostgreSQL function using SQLAlchemy?

All of my tables defined in SQLAlchemy have an id column of type UUID. from sqlalchemy.ext.declarative import declarative_base from uuid import uuid4 Base = declarative_base() class MyTable(Base):...

Show Detail

How to set a column default to a PostgreSQL function using SQLAlchemy?

All of my tables defined in SQLAlchemy have an id column of type UUID. from sqlalchemy.ext.declarative import declarative_base from uuid import uuid4 Base = declarative_base() class MyTable(Base):...

Show Detail