Skip to content

Instantly share code, notes, and snippets.

@jgillman
Last active March 8, 2024 17:51
Show Gist options
  • Star 89 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save jgillman/8191d6f587ffc7207a88e987e034b675 to your computer and use it in GitHub Desktop.
Save jgillman/8191d6f587ffc7207a88e987e034b675 to your computer and use it in GitHub Desktop.
pg_restore a local db dump into Docker
# Assumes the database container is named 'db'
DOCKER_DB_NAME="$(docker-compose ps -q db)"
DB_HOSTNAME=db
DB_USER=postgres
LOCAL_DUMP_PATH="path/to/local.dump"
docker-compose up -d db
docker exec -i "${DOCKER_DB_NAME}" pg_restore -C --clean --no-acl --no-owner -U "${DB_USER}" -d "${DB_HOSTNAME}" < "${LOCAL_DUMP_PATH}"
docker-compose stop db
@matthieusieben
Copy link

matthieusieben commented Aug 21, 2018

DB_CONTAINER=db
DB_NAME=dev
DB_USER=postgres
LOCAL_DUMP_PATH="path/to/local.dump"

docker-compose up -d "${DB_CONTAINER}"
docker-compose exec -T "${DB_CONTAINER}"  pg_restore -C --clean --no-acl --no-owner -U "${DB_USER}" -d "${DB_NAME}" < "${LOCAL_DUMP_PATH}"
docker-compose stop "${DB_CONTAINER}"

@aleon1220
Copy link

Thank you @matthieusieben I am still learning about docker composer. I will test this and let you know. the only missing part is how to bring the db container back up (restarting it)

@lud
Copy link

lud commented Mar 23, 2019

Hi @matthieusieben, could you explain to me why does this work with a local path instead of a path in the container ? The commands runs in the container, right ?

@lashae
Copy link

lashae commented May 23, 2019

@lud Since you run the command docker-compose exec ... on your computer, that < operator is interpreted on your computer as well not on the container ;-) I mean; the command to be run on the container is: pg_restore -C --clean --no-acl --no-owner -U "${DB_USER}" -d "${DB_NAME}" w/o the < "${LOCAL_DUMP_PATH}" suffix; that suffix is run on your computer. The pg_restore is run w/o the --filename parameter, it expects queries to be fed via std-input and < provides data to std-input.

@lud
Copy link

lud commented May 24, 2019

Ok that makes sense, thank you :)

@sirb0rab0g1
Copy link

pg_restore: [archiver] input file is too short (read 0, expected 5) error why ?

@eduardorr97
Copy link

pg_restore: [archiver] input file is too short (read 0, expected 5) error why ?

x2

@eduardorr97
Copy link

pg_restore: [archiver] input file is too short (read 0, expected 5) error why ?

did you ever solved it?

@MaskedBelgian
Copy link

MaskedBelgian commented Mar 24, 2020

Your dump is probably empty or the argument on docker exec are missing.
Could you cat your dump and paste the command you use ?

@ulkoart
Copy link

ulkoart commented Sep 9, 2020

pg_restore: [archiver] input file is too short (read 0, expected 5) error why ?

x2

x3

@swiss-knight
Copy link

Did you forget the -i option in docker command by chance?

@cja-dev
Copy link

cja-dev commented Mar 8, 2024

did somebody solve it? tips above didnt help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment