When running postgresql from docker container, we cannot directly using postgres command in the terminal, we need to use the bash inside the docker container instead.
To see what is the container ID and name
docker ps
After we know the container ID / Name, we can access the bash of the container so we can use the postgresql command.
docker exec -it <container name> bash
Personally, I usually name it with postgres
docker exec -it postgres bash
We will get into the bash container
Then we can use postgres command to export the database
pg_dump -U <username> -d <database_name> -F c > backup.dump
After the exporting database success, the backup.dump
file is still in the container. We need to move it to our system
Exit from the container bash using exit
, then copy the file inside the container
docker cp postgres:output.dump ./output.dump