If you are able to get a good copy of your PG_DATA directory (usually at something like /var/lib/postgresql/data/
) but need to "move" the data contents (the objects themselves plus their contents i.e. tables with rows of data) then here are some instructions and example containers you can use in order to do that.
These instructions are based on the idea that you will use the docker-compose.yaml
file in this repository with Docker Compose in order to create your own local database using the old file system, export the contents of the database to a plain text SQL file using the pg_dump
command, and then you can run that plain text SQL file in your target database.
This example setup uses the database named "root" and the owner/user named "keycloak".
These steps should be followed:
-
Get your hands on the files themselves!
-
Unpack them to the local
./backup/
folder here so that what sits directly under./backup/
should be the actual database file contents themselves (config files likepostgresql.conf
, etc plus folders likebase
,global
, etc). -
Uncomment the
dumpdb
container from the./docker-compose.yaml
file, adjust as necessary (for example it is currently written to usepostgres:10-alpine
but this should match the version of the original database) and then start the database like this:
docker-compose up -d
- Open a bash prompt into
dumpdb
like this:
docker exec -it dumpdb bash -l
- Ensure you can connect to the database and see all of the right data. For example like this:
# Log in locally with user as specified in POSTGRES_USER
psql -U keycloak
# List all databases and see that the right databases are there
\l
# Connect to and list all tables in a specific database if you like
\connect root
\d
# Optionally read data from some table
select * from public.realm;
# Quit psql
\q
- Export the database of your choice to a file using
pg_dump
:
# Connect with user "keycloak" and dump "root" database to file /var/lib/postgresql/data/pgdump.sql
pg_dump -U keycloak -d root -f /var/lib/postgresql/data/pgdump.sql
- From there you can exit the Docker container terminal (
exit
) and then copy the dumped file to your local environment.
docker cp dumpdb:/var/lib/postgresql/data/pgdump.sql ./restore/pgdump.sql
-
Tweak the contents of
./restore/pgdump.sql
as necessary. For example, at the top of the file will likely be some server or database-level stuff that you might not want in the new target database (SET
various parameters, creating database-level extensions, etc) so these can be commented out as needed. -
Make sure that the right user is being set in the
ALTER ... OWNER TO ...;
commands and/or adjust your target database to match this user. -
Copy or mount in the
pgdump.sql
file to your target database. In this exampledocker-compose.yaml
file you can comment out thevolumes
section of thekeycloakdb
pod and then restart it. -
Open a prompt in your new target database and run the file into your new database.
# Check that your file exists and looks good
head -n 30 /tmp/restore/pgdump.sql
# Check that you can connect to your new database with the right POSTGRES_USER
psql -U keycloak
# List all databases and see that the right target database is there and ideally check that the owner matches what you expect
\l
\d
# Quit psql
\q
# Now import the pgdump.sql file into your root database
psql -U keycloak -d root -f /tmp/restore/pgdump.sql
# There should have been a lot of (hopefully) successful commands (ALTER TABLE etc etc)
# Connect to the target database again and see if it looks like your data looks right
psql -U keycloak
\l
\connect root
\d
select * from public.realm;
\q
Note: In case of issues you might have to occasionally fix the owner on mounted files like
sudo chown -R $USER backup
orsudo chown -R $USER conf
orsudo chown -R $USER restore
in case you get file permission errors when starting the containers.
Ideally in the end you could actually log into Keycloak with this specific solution at http://localhost:8080/auth and see that all of your "restored" data looks right!