Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created May 22, 2020 05:04
Show Gist options
  • Save saswata-dutta/cf2b038b6bb8473dfc0a64703bf22a35 to your computer and use it in GitHub Desktop.
Save saswata-dutta/cf2b038b6bb8473dfc0a64703bf22a35 to your computer and use it in GitHub Desktop.
read data from postgres
import psycopg2
from datetime import datetime
def printline(r):
correspondence_id, account_id, case_id, created_date, internal_external_flag, to_line, from_line, cc_line = r
print(f"{correspondence_id},{account_id},{case_id or ''},{created_date.isoformat()},{internal_external_flag},{to_line},{from_line},{cc_line or ''}")
######
user = 'username'
passwd = 'pass'
url = 'hostname'
conn = psycopg2.connect(host=url, port = 8194, database="bpmsar2p", user=user, password=passwd, sslmode='require')
cursor = conn.cursor(name='reader')
query = """select
correspondence_id, account_id, case_id, created_date, internal_external_flag, to_line, from_line, cc_line
from garc.correspondence
where
correspondence_type = 'EMAIL'
and direction = 'IN'
and created_date >= timestamp '2020-02-01 00:00:00'
order by created_date asc
;"""
cursor.execute(query)
while True:
records = cursor.fetchmany(size=100)
if not records:
break
for r in records:
printline(r)
cursor.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment