Docker PostgreSQL database move
Moving a Docker PostgreSQL database. To another drive or another or a new server – or PC.
You can also use it to migrate volumes / bind mounts and vice versa – here’s how! 👍
You can also simply copy a simple volume as described in this article:
It doesn’t matter for what purpose or application you created the Postgres database or what it is used for. In this example, I am using my “paperless” Postgres database.
Notes on data backup
When processing storage media: hard disk, virtual hard disk, USB stick, volume, etc., data may be lost, which can have several causes. Liability is generally excluded.
If you follow the topics and examples discussed in this article and use them, I strongly recommend that you make a data backup beforehand!
Traditional method
I work with running databases here. You cannot simply copy databases while they are running. Please pay attention to how the examples are used here. That means in running operation of the container or when the database container is shut down!
Backup of the running database
Now there is the possibility to do the whole thing via backup. The sample field is as follows:
docker exec [Container] pg_dumpall -U [Postgres User] > ./[Ziel]
- “pg_dumpall” = Back up all databases with users and rights
- Container = Container ID
- Postgres user = root user / user of the database
- Target = Target e.g. mount point “./backup” or drive “D:/backup”
You can look up the container ID with the following command:
docker ps
Now, as an example, the command line would look like this:
docker exec 152e9780542e pg_dumpall -U paperroot > "D:\Backup Datenbanken\paperless\all_postgres.sql"
If, like me, you use a directory structure in which “spaces or umlauts occur”, put the path in “quotation marks” as in the example.
Copying the running container
cThe sample command line would look like this:
docker run --rm --volumes-from postgres-container -v $(pwd)/backup:/backup busybox cp -a /var/lib/postgresql/data/. /backup/
- “–rm” = The container is deleted again after completion
- “–volume-from” = Use the container of a database
- “postgres-conteiner” = The name of the database container, normally this is “DB”. You can also check this again with the command “docker ps”.
- “-v” = the volume to be mounted
- “busybox” = The image to be used for the container
- “cp -a /var/lib/postgres/data/. /backup/ = Copy all data from / to
Now, as an example, the command line would look like this:
docker run --rm --volumes-from paper-db -v d:/backup:/backup busybox cp -a /var/lib/postgresql/data/. /backup/
- “paper-db” = Since I have several data banks running, the container is not called DB for me, but “paper-db”
- “d:/backup:/backup” = I would like to have the copy of the database under “d:/backup
- “/var/lib/postgresql/data/.” = The data path in the Docker container where the data is located. You can find the path in your Docker configuration under Mount / Volumes. E.g. in the docker-compose.yml or in Portier etc.
My way to migrate / move the database
You can follow the process in detail, with explanations, in the video:
Video: Docker PostgreSQL database move
Language: 🇩🇪
☝️ Use YouTube subtitles for all languages.
- Exit the app, i.e. not only the DB container but also the containers that use the database
- Adjust the configuration as required, for example in the “docker-compose.yml” or in the “Portainer”
- Copy the database to the appropriate target. Make sure that your system also displays and copies the “hidden” and “system files”. If the copy is not complete, it will not work. The database must also not be running!
- Start the app again with all containers. Pay attention to the order in which the containers are started (dependencies)
- Check your “Paperless” application here, e.g. for complete data and freedom from errors!
- Lift the original database (volume or mount point noch einige Zeit auf) fall verspätetet Fehler auftreten.
Link to support / donation for the channel
PayPal Link
Bank transfer, Bitcoin and Lightning
#Docker #DockerVolume #MountPoint #Migration #DockerMountPoint #Volume #DockerData #Postgres #PostgreSQL #PostgresMigrate #PostgreSQLMigrate #PostgresMove #PostgreSQL #PostgressDB #PostreSQLdb