Skip to content

Instantly share code, notes, and snippets.

@ricjcosme
Created September 13, 2017 17:33
Show Gist options
  • Star 92 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save ricjcosme/cf576d3d4272cc35de1335a98c547da6 to your computer and use it in GitHub Desktop.
Save ricjcosme/cf576d3d4272cc35de1335a98c547da6 to your computer and use it in GitHub Desktop.
DUMP / RESTORE PostgreSQL Kubernetes
DUMP
// pod-name name of the postgres pod
// postgres-user database user that is able to access the database
// database-name name of the database
kubectl exec [pod-name] -- bash -c "pg_dump -U [postgres-user] [database-name]" > database.sql
RESTORE
// pod-name name of the postgres pod
// postgres-user database user that is able to access the database
// database-name name of the database
cat database.sql | kubectl exec -i [pod-name] -- psql -U [postgres-user] -d [database-name]
@hseyindemir
Copy link

Hi, I need to migrate a postgresql database from a k8s environment to an on-premise server. Can i do this by exporting .sql file from k8s and running this .sql file on the on-premise server ?

@naviat
Copy link

naviat commented Jan 2, 2019

Hi, I need to migrate a postgresql database from a k8s environment to an on-premise server. Can i do this by exporting .sql file from k8s and running this .sql file on the on-premise server ?

I think it's the same scenario, you can use the script above.

@sudonewdev
Copy link

I'm facing authentication failed using RESTORE

@ayoubhakik
Copy link

@sudonewdev try this
kubectl exec -i [pod-name] -- psql -U [postgres-user] -d [database-name] -f database.sql
or install psql client with :

sudo apt install postgresql-client-common
sudo apt-get install -y postgresql-client

then :
psql -U [postgres-user] -p [database-port] -h [postgres-ip] -d [database-name] -f database.sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment