How to get started with PostgreSQL using Docker
In my previous blog post on how to get started and install postgresql on windows, well for those on Mac or Linux users or for those who want a much quicker way of getting PostgreSQL up and running, there is another way!
This will be a short blog post on how to use Docker and specifically Docker Compose to have PostgreSQL up and running in minutes.
This is ideal for local development and especially much easier for onboarding developers on the team as they may not have PostgreSQL installed and its just a case of adding this simple instruction and a docker compose file which I will go through next.
Docker Compose file for PostgreSQL
Below is the contents of a docker compose file which will get you up and running. It includes a
volumes section. This is so that if you restart your machine or docker, the data within PostgreSQL is retained.
Set the username and password
The username and password I have set is using the environment variables
POSTGRES_PASSWORD feel free to change these to your needs.
version: '3.5' services: postgres: container_name: postgres_container image: postgres environment: POSTGRES_USER: admin_user POSTGRES_PASSWORD: ZWfBD7WV1pD99R47sfZQAMY5nQ PGDATA: /data/postgres volumes: - postgres:/data/postgres ports: - "5432:5432" volumes: postgres:
Get it up and running
Copy the contents of the docker compose above and save it in a file such as
Then using a terminal of your choice and assuming you have Docker installed already, use the following command to get it up and running.
docker-compose -f docker-compose-postgresql.yml up -d
To check if its running we can run the docker process command.
You should see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a2b2ad271a0b postgres "docker-entrypoint.s…" 3 days ago Up 2 seconds 0.0.0.0:5432->5432/tcp postgres_container
To stop it, you can use the docker stop command.
docker stop postgres_container
Thats it! Well that was a short post, but I thought I share a quick way of getting PostgreSQL running locally on your machine.