Dumping a PostgreSQL Database without Docker
When migrating systems, you might have been hit with the problem of migrating a PostgreSQL database from a read-only filesystem snapshot.
If you think this sounds like a really specific skill issue, I can assure you that it absolutely is. Anyways, since the switch to NixOS for my main server in August, there were a bunch of docker volumes in my filesystem snapshot, some are psql databases. I don’t have docker/podman installed, so how am I going to export a dump of the databases?
Well, welcome to this post.
Let’s rule out things that won’t work.
- copying the entire directory and paste it right into the main psql database
and that’s about all the things that I can think about.
For the purposes of this guide, we will assume the following variables:
1 | |
Get the database out of a volume
First and foremost, we need to get the database directory out of the volume first. Since the filesystem is read-only, we have no choice but to move it out. You can alternatively do some cool insane stuff like mounting an overlayfs on the volumes directory, but for the purpose of this guide we are not going to do that to keep this simple.
We can simply copy the entire database to an empty temporary database:
1 | |
Now that we have the data dir, change the ownership of $TMPDIR recursively to a user that’s not root, as pg_ctl refuses to run as root.
1 | |
Set some config so postgres doesn’t explode
We then have to replace the port of our working shadow server, so as to not conflict with the main psql database. You can skip this part if you don’t have one running.
1 | |
Additionally, harden the access configuration for the peace of mind.
1 | |
Run the server
Now we are finally ready to run the server. As explained above, we cannot run the database server as root.
1 | |
Dump the database
Chances are the default user (usually the only user) in the database isn’t postgres. If you use docker compose, just figure it out from your compose.yaml.
I unfortunately cannot help you at the moment if you don’t know the database and/or user.
Afterwards, you can simply do the following to get a db dump.
1 | |
The dump is split into 2 files for easier checking, and easier editing as the owner role might not be the same.
If that is the case, simply change all the old owners in the schema.dump.sql to the new one.
1 | |
Dump the dump to the data dumpster (database)
This part is easy. Maybe.
Up to this point, in no commands have we told psql to dump users and the databases. We will now have to create them before loading the database dumps.
1 | |
We can now import the dump.
1 | |
At this point we should have a fully recovered database. We can then move on to removing the temporary files.
Removing the dump
First, pull the plug on the shadow server.
1 | |
Now, just remove the temporary directory with the same method you normally use.
1 | |
That’s all!
Dumping a PostgreSQL Database without Docker
https://b.soopy.moe/2023-11/Dumping-a-PostgreSQL-Database-without-Docker/