postgresql - Python date string to postgres usable date -
issue: stored dates text in postgres table, , want convert on actual dates, again in postgres.
im not sure if there better way or im doing wrong. have pulled bunch of data postgresql database in text format. result need go through , clean up. running issues data. need convert format postgresql can use. went pulling python , trying convert , kick back. best way this? having issue datetime.strptime.. believe i've got directive correct no go. :/
import psycopg2 datetime import datetime # connect postgresql database conn = psycopg2.connect( "dbname='postgres' user='postgres' host=10.0.75.1 password='mysecretpassword'") # create new cursor cur = conn.cursor() cur.execute("""select "hash","date" nas """) # commit changes database mydate = cur.fetchall() rows in mydate: target = rows[1] datetime.strptime(target, '%b %d, %y, %h:%m:%s %p %z')
here postgres query can convert strings actual timestamps:
select ts_col, to_timestamp(ts_col, 'month dd, yyyy hh:mi:ss pm')::timestamp time zone your_table;
for full solution, might take following steps:
- create new timestamp column
ts_col_new
in table - update column using logic above query
- then delete old column containing text
the update might this:
update your_table set ts_col_new = to_timestamp(ts_col, 'month dd, yyyy hh:mi:ss pm')::timestamp time zone;
Comments
Post a Comment