Skip to content

Instantly share code, notes, and snippets.

@amulya349
Created September 10, 2018 10:35
Show Gist options
  • Save amulya349/3fccdd3cf456f2ef473df6a3fb1eb2f9 to your computer and use it in GitHub Desktop.
Save amulya349/3fccdd3cf456f2ef473df6a3fb1eb2f9 to your computer and use it in GitHub Desktop.
This script can be used to copy a CSV file (with header) to a Postgres remote database.
#!/usr/bin/env/python
import psycopg2
# Get a database connection
dsn = "dbname=%s user=%s password=%s host=%s port=%s" % ('db_name','username','password','remote_host_ip','port')
conn = psycopg2.connect(dsn)
query = "COPY public.hiringpattern_new FROM stdin WITH CSV HEADER DELIMITER as ',' "
fp = open('csv_file_path', 'r')
cur = conn.cursor()
cur.copy_expert(sql=query, file=fp)
conn.commit()
cur.close()
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment