postgres+docker(part 1)
I’m planning to add a persistence layer to my ongoing series about Scala+Docker development, so I thought I would take a few minutes and practice with a containerized Postgres.
Here’s the plan:
- launch a Postgres container
- create a table and populate with data (US area codes)
- make some queries
Let’s go! First, start a new directory and add a docker-compose.yml
file ( I prefer docker-compose files over crafting long Docker run
commands)
Here’s the content of docker-compose.yml
. It’s a super-straightforward, standard Postgres setup but we area also adding an new database called ‘geography’.
We also need some demo data. Let’s use this csv file containing US area code data
First let’s create a local dir called data
which we will then mount in our Docker container.
Quick trick to download a file from the terminal:
Now lets mount that data directory in our Postgres container so we can access it from inside the container. Just add volumes
to the bottom of our docker-compose.yml
and specify that we will mount our local ./data
directory in the container at the path /data
:
Alright! We’re ready. Now start the container in the background with docker-compose up -d
.
Once it is running, let’s enter a bash session in the container: docker exec -it postgres bash
Once inside, let’s make sure our data is there:
cat data/us-area-code-cities.csv
Nice. Now lets use the psql
cli tool and have some fun!
Outstanding. This was a great into to Postgres and Docker. I look forward to more posts including making the data persist (right now, it will be gone when the container comes down) and also connecting Postgres to our Scalatra app.
–
NOTE ABOUT POSTGRES SCHEMAS
The term can be confusing. read more about it here