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)
CodePudding user response:
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)
CodePudding user response:
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"')
Add escape characters in the comma \',\'
between DELIMITER and CSV to be recognizable by Python:
os.system('psql -U postgres -c "\COPY (SELECT * FROM "public"."tableName") TO tableName.csv DELIMITER \',\' CSV"')