Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaytaylor/7897774b1abbeaf45b1529887ce72fbf to your computer and use it in GitHub Desktop.
Save jaytaylor/7897774b1abbeaf45b1529887ce72fbf to your computer and use it in GitHub Desktop.
How to insert a timestamp without timezone into postgres using Python

HOWTO: Insert a timestamp without timezone into postgres using Python

Background

I searched the net high and low but was unable to locate a definitive working example of how to take an epoch integer timestamp and insert it as a native postgres timestamp without timezone.

CREATE TABLE IF NOT EXISTS tz_test (
    stamp TIMESTAMP NOT NULL
);
import time
import psycopg2

# ... snip ...

epoch = 1637599938

stamp = time.strftime("%Y-%m-%d %H:%M:%S", datetime.datetime.utcfromtimestamp(stamp).timetuple()),

cursor.execute('''INSERT INTO tz_test (stamp) VALUES (%s)''', stamp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment