How to Set Up a Postgresql Database Container with Docker Machine

2020-07-17 13:50:05 | #sysadmin #database

Tested On

  • Linux Ubuntu 20.04

Problem: You want to quickly set up a Postgresql database for development purposes. You also want the database to run in a container so that it's free of conflicts and easy to clean up. Finally, when you're finished programming and testing, you want to be able to spin down and clean up all of the database resources in a repeatable way.

Solution: Docker Machine and Docker containerization. With just a few short commands, we'll have a Postgresql database running in an isolated container that we can easily connect to and run queries against.

Creating the Postgresql DB Container

Follow these instructions to install docker-machine on your preferred operating system. Make sure to replace Ch4ng3M3 with your preferred password.

# Create virtual machine
docker-machine create --driver virtualbox --virtualbox-disk-size "40000" dev

# Set environment variables
docker-machine env dev
eval "$(docker-machine env dev)"

# Create postgresql database container (make sure to replace Ch4ng3M3 with your preferred password)
docker run --name pgdb -e POSTGRES_USER=pguser -e POSTGRES_PASSWORD=Ch4ng3M3 -e POSTGRES_DB=pgdb -p 5432:5432 -d postgres

# List containers to confirm db is running
docker ps

Your terminal should output the following information about your docker container:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
62f3577b7e10        postgres       "docker-entrypoint.s…"   22 seconds ago      Up 1 second         0.0.0.0:5432->5432/tcp   pgdb

If you get the following error, this means something is already running on port 5432. It could be another postgres instance, a postgres docker container, or a separate process entirely.

docker: Error response from daemon: driver failed programming external connectivity on endpoint pgdb (ceb965e08c68a3e38f868346a2be60fd0dae125ed0685e8a5ac3c202ac70cd4c): Bind for 0.0.0.0:5432 failed: port is already allocated.

Please refer to this article to identify and terminate the process running on port 5432. If you would rather keep that existing process running, you can instead run the pgdb docker container on a different port, such as 5433, with the following commands.

# Delete the pgdb container
docker rm -f pgdb

# Run pgdb container on port 5433
docker run --name pgdb -e POSTGRES_USER=pguser -e POSTGRES_PASSWORD=Ch4ng3M3 -e POSTGRES_DB=pgdb -p 5432:5433 -d postgres

Connecting to the DB Container

# Get the virtual machine's IP address
docker-machine ip dev

# Identify which port the pgdb container is running on
docker ps

# Connect to the DB (enter your password when prompted)
# Make sure to substitute IP_ADDRESS with the dev docker machine's ip...
# ...and PORT_NUMBER with the pgdb container port
psql -h IP_ADDRESS -U pguser -d pgdb -p PORT_NUMBER -W

Once you're connected, you'll be able to run your queries. If you want to connect from a program you've written, you can use the same connection parameters. For an example of how to do this with Python 3, check out this article.

Want To See More Exercises?

View Exercises

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to comments for this post

Want To Receive More Free Content?

Would you like to receive free resources, tailored to help you reach your IT goals? Get started now, by leaving your email address below. We promise not to spam. You can also sign up for a free account and follow us on and engage with the community. You may opt out at any time.



Tell Us About Your Project









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Hire Us

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

Contact