Home > Mobile >  How can I use pg_dump in Kubernetes in order to generate dump from a remote PostgreSQL (PGAAS)?
How can I use pg_dump in Kubernetes in order to generate dump from a remote PostgreSQL (PGAAS)?

Time:02-04

I would like to generate dump from a remote PostgreSQL database (PGAAS) with commands or Python code.

Firstly I tried locally to do the work but I have an error :

pg_dump: error: server version: 13.9; pg_dump version: 12.12 (Ubuntu 12.12-0ubuntu0.20.04.1)

I tried this code :

import subprocess

dump_file = "database_dump.sql"

with open(dump_file, "w") as f:
    print(f)
    subprocess.call(["pg_dump", "-Fp", "-d", "dbdev", "-U", "pgsqladmin", "-h", "hostname"-p", "32000"], stdout=f)

How can I do to have a pod (container) doing this work and where version is the same that server version, without entering pgaas password manually ?

CodePudding user response:

pg_dump: error: server version: 13.9; pg_dump version: 12.12 (Ubuntu 12.12-0ubuntu0.20.04.1)

As you can see this error is caused because of a version mismatch checking the version of your PGaaS database and the database version you are using on your local machine. If your local version is lower than that of the server version you can upgrade the local version. Follow this document for upgrading your pg version.

If you want to take dumps at regular intervals in an easy way you can have a cron job scheduled on your vm for running your code. Since you want to use kubernetes, build a docker image with your code in it and create a kubernetes job and run it with kube-scheduler and you can use environment variables for encrypting your password.

  • Related