How to dump Postgres Database with Docker
I am a dedicated software engineer with a deep passion for security and a commitment to developing robust and scalable solutions. With over three years of hands-on experience in the .NET ecosystem, I have built, maintained, and optimized various software applications, demonstrating my ability to adapt to diverse project needs. In addition to my expertise in .NET, I have six months of specialized experience working with Spring Boot and ReactJS, further broadening my skill set to include full-stack development and modern web technologies. My professional journey includes deploying small to medium-sized systems to cloud platforms and on-premises environments, where I have ensured reliability, scalability, and efficient resource utilization. This combination of skills and experience reflects my versatility and commitment to staying at the forefront of the ever-evolving tech landscape.
In this special case, the server doesn't have Docker installed but needs the pg_dump command to export the database. If the pg_dump version does not match, you get this error:
pg_dump: error: aborting because of server version mismatch
You can upgrade the Postgres database version, but sometimes installing the pg_dump command does not work because the network firewall rules block the repositories.
The solution is here
docker pull postgres
docker run -it --rm --user root postgres bash
Inside the docker container, you run this command
pg_dump -h <db_hostname> -U <db_username> -f <dbname>.sql <dbname>
pg_dump -Fc -v -h <db_hostname> -U <db_username> <dbname> > <dbname>.dump
If the password is correct, a .sql file will be generated inside the root folder of the container. You can copy this file to the host machine with the following command:
docker cp <containerId>:/file/path/within/container /host/path/target