I need to insert rows into PG one of the fields is date and time with time stamp, this is the time of incident, so I can not use –> current_timestamp function of Postgres at the time of insertion, so how can I then insert the time and date which I collected before into pg row in the same format as it would have been created by current_timestamp at that point in time.
Python – How to insert current_timestamp into Postgres via python
datetimepostgresqlpython
Related Topic
- Python – How to get the current time in Python
- Python – “Least Astonishment” and the Mutable Default Argument
- Python – How to concatenate two lists in Python
- Postgresql – Postgres: INSERT if does not exist already
- Javascript – How to sort an object array by date property
- Python – Create a Pandas Dataframe by appending one row at a time
- Sql – Microsoft Access ADP UPDATE Query does NOT update
Best Answer
A timestamp does not have "a format".
The recommended way to deal with timestamps is to use a PreparedStatement where you just pass a placeholder in the SQL and pass a "real" object through the API of your programming language. As I don't know Python, I don't know if it supports PreparedStatements and how the syntax for that would be.
If you want to put a timestamp literal into your generated SQL, you will need to follow some formatting rules when specifying the value (a literal does have a format).
Ivan's method will work, although I'm not 100% sure if it depends on the configuration of the PostgreSQL server.
A configuration (and language) independent solution to specify a timestamp literal is the ANSI SQL standard:
Yes, that's the keyword
TIMESTAMP
followed by a timestamp formatted in ISO style (theTIMESTAMP
keyword defines that format)The other solution would be to use the
to_timestamp()
function where you can specify the format of the input literal.