简体   繁体   中英

How to PSQL COPY with PYTHON

Trying to run a PSQL COPY command using python. I'm able to connect to the right DB in the given kube cluster.

The lose track of the shell once im on postgres. When i exit, i get the following error. Is there any wrong with the quotes that I'm using in the PSQL command in the os.system call.

import os
import subprocess

conn = 'gcloud container clusters get-credentials clusterA --zone us-west --project projectX'
pg = 'kubectl -n DB exec -it postgres -- bash'


if __name__ == '__main__':

   subprocess.call(back_clus_conn, shell=True)
   subprocess.call(pg, shell=True)
   os.system('psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER ',' CSV"')

ERROR:

 kubeconfig entry generated for clusterA.
 Defaulted container "postgres" out of: postgres, postgres-exporter
 bash-5.0#
 bash-5.0# exit
 exit
 Traceback (most recent call last):
 File "test.py", line 19, in <module>
 os.system('psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER ',' CSV"')
 TypeError: system() takes exactly 1 argument (2 given)

You can just run the command in the postgres container exec subprocess call, something like this:

import subprocess

conn = 'gcloud container clusters get-credentials clusterA --zone us-west --project projectX'
copy_cmd = 'psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER ',' CSV"'
pg = f'kubectl -n DB exec -it postgres -- bash -c {copy_cmd}'

if __name__ == '__main__':
   subprocess.call(back_clus_conn, shell=True)
   subprocess.call(pg, shell=True)



Based on sticky bit , It tells you to give it two arguments instead of one.

Currently your code is:

 os.system('psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER ',' CSV"')

You may try to Add \',\' to be recognizable by Phyton

os.system('psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER \',\' CSV"')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM