Running postgres in docker8/24/2023 I hope this note-to-self quickie will be of some help to you as-well. These steps are mostly for moving development data around or pulling (partial) production data locally for debugging, or something along those lines. If you use any of these manual steps as a means to create backups, you're probably doing something not entirely correct. Please keep in mind that you should always ensure your production databases are properly backed up, and ideally automatically so. There are certainly other ways to achieve something similar, but this method will work in a pinch. manually created) container to another, you could use pipes to do this in one command, like so: docker exec -i pg_old_container_name /bin/bash -c "PGPASSWORD=pg_password pg_dump -username pg_username database_name" | docker exec -i pg_new_container_name /bin/bash -c "PGPASSWORD=pg_password psql -username pg_username database_name" Conclusions If you, for example, are moving data from one (e.g. If you would instead prefer to stop the import completely upon error, be sure to add -set ON_ERROR_STOP=on to your above command. The dexec bash script simplifies the process of accessing and interacting with Docker containers. Note: By default PostgreSQL keeps importing even when errors occur. Note: If you are attempting to restore data from a custom format dump, you should instead use pg_restore as I described in my How to set up and use Postgres locally article. Since you are not able to provide a password directly through arguments, we rely on the PGPASSWORD environment variable: docker exec -i pg_container_name /bin/bash -c "PGPASSWORD=pg_password psql -username pg_username database_name" < /path/on/your/machine/dump.sql Dump using pg_dump docker exec -i pg_container_name /bin/bash -c "PGPASSWORD=pg_password pg_dump -username pg_username database_name" > /desired/path/on/your/machine/dump.sql Restore using psql This quickie assumes you have nothing directly installed on your development machine, so everything is run straight from and to the Docker PostgreSQL container you're running. Depending on why you need to dump/restore a database, this might help for you, too. I ran into this just today, and thought I'd share one method that I felt was easy, fast and served my purpose. : Updated the guide with a more up-to-date method for sending a password along with the commands. Ports : - 5432 : 5432 #Using a custom DockerfileÄocker-compose.yml version : "3" services : postgres : build. To exit the shell: exit #Overriding configuration parameters version : "3" services : postgres : image : "postgres" container_name : "postgres" command : - "postgres" - "-c" - "log_connections=yes" - "-c" - "max_connections=1337" environment : - POSTGRES_USER=john project - the database you want to connect to.If you are a Linux user and need help setting up, refer to one of our guides on installing Docker Compose on Ubuntu or how to install Docker Compose on CentOS. docker exec -it - run a command to a running container Option 1: Run Postgres Using Docker Compose To deploy a Postgres container using Docker Compose, you should have this Docker tool set up on your system.To access the shell: docker exec -it postgres psql project -U john To start the service: docker compose up -d Connect to the postgres instance on localhost:5999 - you can see in the ports section we forward this port to container port 5432 - the port that Postgres is running on. sql scripts to run Run docker-compose up to run it, or docker-compose up -d to run the container in the background. Note: a default database called postgres will be created. Create the directory psqlinit and populate it with. Docker-compose.yml version : "3" services : postgres : image : "postgres" container_name : "postgres" environment : - POSTGRES_USER=john
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |