Skip to content

Instantly share code, notes, and snippets.

@kshwetabh
Created January 11, 2018 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshwetabh/df21b180aafc224b8748a59e983fe578 to your computer and use it in GitHub Desktop.
Save kshwetabh/df21b180aafc224b8748a59e983fe578 to your computer and use it in GitHub Desktop.
Use this script to load NIFTY Intraday data into PostgreSQL Database
import psycopg2
conn = psycopg2.connect("host='localhost' port='5432' dbname='stocks' user='xxxxxx' password='xxxxxx'")
cur = conn.cursor()
SQL_STATEMENT = """
COPY %s FROM STDIN WITH
CSV
HEADER
DELIMITER AS ','
"""
def process_file(conn, table_name, file_object):
cursor = conn.cursor()
cursor.copy_expert(sql=SQL_STATEMENT % table_name, file=file_object)
conn.commit()
cursor.close()
f = open(r'C:\Users\xxx\Desktop\AllStocksExtracted\code\result.csv', 'r')
try:
process_file(conn, 'stocks', f)
finally:
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment