I have a Postgresql data dump file. How can I restore that backup into a data only container in the way that the restore is in place when I docker run the postgresql container?
2 Answers
As Stackoverflow penaltizes answers which are too succinct I repeat what is written there:
- Create a data only container
- Run a temporary Postgres container to restore the backup. Attach the data only container AND the directory containing the local backup.
- Attach to the restore container. Within the container,
- Switch to the postgresql user
- Create the superuser and matching password for the database to restore.
- Exit the psql session with \q. Still within the container,
- restore the backup on the command prompt
- Create the final Docker Postgres container. Note to specify the correct name of the database the restore has been made into from the previous step.
- delete the container used to restore the backup into the data only volume
1 Comment
Sarz
Your answer will get more meaningful if you also show us example commands on each/mandatory steps!! Cheers
The easiest would be to create an empty postgresql database on the host in separate folder, then start the PostgreSQL daemon, then import the data and then simply mount it to the container. Using the script below you can create the DB in /tmp/postgres-mem folder and then import data into it.
#!/bin/bash
export PGDIR=/tmp/postgres-mem
pg_ctl -D "$PGDIR" stop
rm -vfr "$PGDIR"
mkdir "$PGDIR"
pg_ctl init -D "$PGDIR"
pg_ctl -D "$PGDIR" start -o "-h localhost -c fsync=off -c synchronous_commit=off -c full_page_writes=off -c log_destination='stderr' -c logging_collector=on -c log_directory='pg_log' -c log_filename='postgresql-%Y-%m-%d_%H%M%S.log' -c log_statement='all' -k /tmp/postgres-mem"
sleep 3
psql -h localhost -c 'create user cooluser with superuser' template1
psql -h localhost -c 'create database "whatever"' template1
and then
psql -U cooluser whatever < your_dump_file.sql
then you copy the content of /tmp/postgres-mem into your data container
However you may want to consider this approach.
2 Comments
JohnDoe
The originating PostgreSQL instance was not Dockerized so the approach you suggest in However will not work.
jdevelop
Okay, still you need to get some filesystem initialized with the database for the specific postgresql instance. And it doesn't really matter how would you get it :)