Hi I am making a cloud function on gcp with python, basically I want to make a backup of one table from postgres database using pg_dump instruction. Once I make the backup i get the result and store the file into one bucket. I have my bucket name in one secret (secrets manager).
from curses import meta
import logging
import datetime
import os
import subprocess
import shutil
import gzip
import psycopg2
from google.cloud import storage
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import functions_framework
dev_bucket=os.environ.get('migration_bucket')
storage_client = storage.Client()
bucket = storage_client.get_bucket(dev_bucket)
def backup_specifc_table():
"""
Backup postgres db to a file.
"""
try:
connection = psycopg2.connect(
database= 'my_db',
user= 'my_user',
password= 'my_pass',
host='my host',
port=5432,
sslmode="require")
cursor = connection.cursor()
print(cursor)
print('here')
cursor.execute("pg_dump --no-owner --data-only --schema=core my_db")
data=cursor.fetchone()
print(data)
blob = bucket.blob(my_backup.sql)
blob.upload_from_string(data, 'text/plain')
return data
except Exception as e:
print(e)
exit(1)
When I test my cloud function showed me this error:
2022-09-12 17:55:56.613 CSTsql_migrationo4spbtixlos2 Traceback (most recent call last): File "/workspace/main.py", line 104, in backup_specifc_table cursor.execute("pg_dump --no-owner --data-only --schema=core my_db") psycopg2.errors.SyntaxError: syntax error at or near "pg_dump"
Traceback (most recent call last): File "/workspace/main.py", line 104, in backup_specifc_table cursor.execute("pg_dump --no-owner --data-only --schema=core my_db") psycopg2.errors.SyntaxError: syntax error at or near "pg_dump"
This is my requirements.txt
psycopg2-binary
boto3
psycopg2==2.9.3
Flask==2.2.2
google-api-core==2.8.2
google-auth==2.11.0
google-auth-oauthlib==0.5.2
google-cloud-build==3.9.1
google-cloud-core==2.3.2
google-cloud-functions==1.8.1
google-cloud-storage==2.5.0
google-crc32c==1.3.0
google-resumable-media==2.3.3
googleapis-common-protos==1.56.4
greenlet==1.1.2
grpc-google-iam-v1==0.12.4