我需要从远程服务器读取所有CSV文件,并将数据插入到远程PostgreSQL数据库。 我编写了代码以读取所有CSV文件并将其插入数据库中,但是它只能在本地运行并经过测试,并且运行良好。
当我尝试使用paramiko从远程服务器读取它时,我无法执行此操作,尝试了google中提到的一些操作,并且无法从源读取文件并将其移植到目标。 / p>
任何指导都将对解决此问题有很大帮助。
import csv
import psycopg2
import os
import paramiko
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target_host = 'site_name.com'
usr_name = 'root'
ssh.connect( hostname = target_host , username = usr_name )
print("SSH connection succesful")
except (Exception, psycopg2.Error) as error:
print (error)
quit()
csv_filepath ="/users/lins/home/csv"
try:
conn = psycopg2.connect(host="localhost", database="postgres", user="postgres",password="*******", port="5432")
print('Database connected succesfully',"\n")
cur = conn.cursor()
except (Exception, psycopg2.Error) as error:
print (error)
quit()
EXT = "*.csv"
all_csv_files = [file
for path, subdir, files in os.walk(csv_filepath)
for file in glob(os.path.join(path, EXT))]
for fp in all_csv_files:
filePath=fp
if os.path.isfile(filePath):
try:
sql = "COPY %s FROM STDIN WITH DELIMITER AS ';' csv header"
file = open(filePath, "r" , encoding="latin-1")
table = 'dl.dhl_tracking'
cur.copy_expert(sql=sql % table, file=file)
print('Data porting done',"\n")
except (Exception, psycopg2.Error) as error:
print (error)
quit()
else:
quit()
conn.commit()
cur.close()
conn.close()