Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pg_dump in Docker container

I am trying to run pg_dump in a Docker container via kubectl and save the output to my local machine.

Here's what I have so far:

kubectl exec -it MY_POD_NAME -- pg_dump -h DB_HOST -U USER_NAME SCHEMA_NAME > backup.sql

However this just hangs currently. I am fairly certain it's due to the -- ignoring the >

kubectl exec -it MY_POD_NAME -- pg_dump -h DB_HOST -U USER_NAME SCHEMA_NAME outputs to the console as expected.

like image 369
timothyclifford Avatar asked Sep 06 '25 16:09

timothyclifford


2 Answers

Use kubectl port-forward POD_NAME 6000:5342 to forward your pod port (assumed to be exposed on 5432) onto localhost:6000.

And then run pg_dump directly with hostname as localhost and port as 6000

$ pg_dump -h DB_HOST -U USER_NAME SCHEMA_NAME > backup.sql

like image 108
tselvan Avatar answered Sep 08 '25 07:09

tselvan


Managed to solve myself - not the most elegant solution but it works.

First I open a shell on a pod in the cluster which has network access to the RDS instance:

kubectl exec -it psql-xxx-xxx sh

Once connected to the shell, run pg_dump to backup the database:

pg_dump -h db.internal.dns -U user schema_name > backup.sql

Once the backup completes, exit the container and copy the file from the pod to my local:

kubectl cp psql-xxx-xxx:/backup.sql ./backup.sql

Will continue searching for a streamlined way to do this.

like image 25
timothyclifford Avatar answered Sep 08 '25 05:09

timothyclifford