3

PostgreSQL supports specifying Date Formats using the DateStyle Property as mentioned here, http://www.postgresql.org/docs/current/interactive/runtime-config-client.html#GUC-DATESTYLE. (link was originally to 8.3 version of docs).

I could not find any SQLAlchemy ORM documentation reference on to how to define this property. Is it possible to do it?

1 Answer 1

5

SQLAlchemy makes use of the DBAPI, usually psycopg2, to marshal date values to and from python datetime objects - you can then format/parse any way you want using standard python techniques. So no database-side date formatting features are needed.

If you do want to set this variable, you can just execute PG's SET statement:

conn = engine.connect()
conn.execute("SET DateStyle='somestring'")
# work with conn

to make this global to all connections:

from sqlalchemy import event
from sqlalchemy.engine import Engine

@event.listens_for(Engine, "connect")
def connect(dbapi_connection, connection_record):
    cursor = dbapi_connection.cursor()
    cursor.execute("SET DateStyle='somestring'")
    cursor.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works. In the meantime as mentioned by you i had used the python datetime formatting operations which are working fine for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.