Containerisation has become a common practice of modern software development after its introduction a decade ago. It allowed developers to package applications together with their dependencies into reproducible, isolated units. Databases are no exception — and PostgreSQL, with its stability and rich SQL features, is a go-to choice for many.
In this article, we’ll walk through how to create a custom PostgreSQL Docker image that automatically initialises your schema and loads snapshot data when the container starts for the first time.
If you haven’t seen it yet, you might also want to read Creating a Custom MySQL Docker Image with Initialization Scripts — the process is quite similar, but there are a few PostgreSQL-specific nuances we’ll cover here.
Here is how we are going to do it step by step:
- Create a Dockerfile that extends the official PostgreSQL image
- Add your SQL initialization scripts
- Build and run the image with a new database instance
- Verify that the database and data are loaded correctly