I needed to back up a PostgreSQL database as a part of the repro for an issue I had. I hadn’t ever made a backup of PostgreSQL, so this was a learning exercise for me. Plus, a container made it slightly more complex than SQL Server. This post shows what I did.
In looking over the documentation and in searches, everyone seems to use pg_dump to make a backup. This looks hokey and immature to me, essentially a command line tool to script things out.
There is also a file level backup and a PITR backup strategy, but those are more complex for my use case.
So, how do I run pg_dump?
I found this article, which is helpful, but contains a lot of stuff. Essentially, I need to connect to my container and run from there. I’ll use the exec with the it switch from Docker to do this.
So, first I run this to get a shell inside the container.
docker container exec -it pgdev /bin/bash
The image below shows me connected to the container with a bash shell.
Now I can run pg_dump. I’ll use this command, which connects to the db with a user and sends a database backup to the /usr location.
pg_dump -U postgres -Fc bb_fullrestore > /usr/bbfull.dmp
Once this is done, I can go check. First, I’ll ls this folder and I see my file.
Summary
As technology advances and I use containers more, I’m hesitant to keep installing stuff on my machine that I don’t need to. I’d rather have scripts. Trying to just get pg_dump installed is a pain, so this post shows how you can access this in a container and create a backup.
If you’ve mapped your folders in a container to your local machine, then you can easily find this file and most it elsewhere for a restore.