Setting up Postgres inside a Docker container
I've been doing a bit of work with Postgres recently, and encountered a few gotchas whilst getting things set up inside a Docker container for the first time. I thought I'd share my final process here, just in case it comes in handy for the equally uninitiated.
I'll focus on setting up a very simple database for local development purposes, and then hopefully you'll be able to build on it and create something useful for yourself.
For anyone that wants to dive straight into a working example, please check out my github repo. Just be aware that the repo uses scripted versions of the commands that I describe in this article.
By the end of this article, you should be able to:
- run Postgres inside a Docker container
- configure the Dockerised Postgres instance, and set up a simple database
- OSX Mojave
- Existing Docker installation, plus basic knowledge of this technology
- Basic knowledge of relational databases
Set up the project
First of all, let's set a couple of environment variables for use inside the Postgres container. It's far easier to manage these values by keeping them together in a file.
Run Postgres as a Docker container
Let's break that command down.
- we've based our container on the official Postgres version 11.1 image:
- chosen to run the container detached from the terminal:
- opened port 5432, which Postgres listens to by default:
- loaded the environment variables from the file we created earlier:
- given the container a name:
Create a User
We can use the built-in psql tool to run commands against Postgres, but we need to let it know which user we wish to connect.
At this point you should be connected as the postgres user, and your terminal prompt should look something like:
Connect to the database
Remember the POSTGRES_DB variable that we entered into our .env file earlier on? Well, the value that we supplied to that variable (hackerbox_test_db) has magically been used to create a database of the same name.
The terminal should confirm the connection with a message similar to:
Create a simple table
Our database is completely bare at the minute, so let's create a table.
If you query the people table at this point, you should see that it doesn't contain any rows:
If you query the people table again, you should see that a row has been inserted and the id has been created automatically:
So that's the "quick and dirty" on how to use Postgres inside a Docker container. You can connect to your running container externally, possibly using a GUI tool such as pgAdmin, by specifying your container ip (this will just be 127.0.0.1 if you're running Docker locally) along with the password that we set in the .env file (thisisapassword).
I hope that you enjoyed reading my article, and that you'll share it around.