I'm running script in Jupyter notebook to upload csv file into Postgresql table. Jupyter notebook is on server1 and postgresql server is on another server server2.
psql client also installed on Jupyter machine(server1). Able to upload file using below psql command from server1
hari@idrtnjdhew1:~/data$ psql -h server2.zzz.com -p 5432 -d db_name -U hari1 -c '\COPY test_upload FROM 'file_name.csv' CSV HEADER'
But I have to write scripts in Jupyter notebooks to upload file into postgresql server table.
import psycopg2 as pg
import os
import sys
conn = psycopg2.connect(
host="server2",
database="db_name",
user="hari1",
password="xyz")
cursor = conn.cursor()
delete = """Truncate table test_upload"""
cursor.execute(delete)
copy_cmd = """
psql -c "\COPY (test_upload) FROM STDIN CSV HEADER" < '\home\data\file_name.csv'
"""
cursor.execute(copy_cmd)
But above scripts is giving error SyntaxError: syntax error at or near "psql" LINE : psql -c "\COPY (test_upload) FROM STDIN...
Is there way to execute psql command using psycopg2