Simple Dockerised Postgres

WN
William Neild
1 mins 195 words

Every time I need to spin up a quick Postgres database for local testing, I find myself forgetting the steps, so I am jotting down the process here to have a reliable reference to look back on.

Postgres with Docker

For a quick and easy setup, the following docker command does the trick:

docker run --name <container_name> -e POSTGRES_PASSWORD=<password> -d postgres

This command creates a new docker container with <container_name> as the title, and the database password being <password>.

Postgres with Docker Compose

Or if you want to get a little more sophisticated and use docker-compose, you can use the following:

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data
    # Optionally export default postgres port if not running service within docker
    #ports:
    #  - 5432:5432

  # Optional webui
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

volumes:
  pgdata:

Then running the following command will start up the Postgres container, along with any other defined services within the docker-compose.yml file (such as the example adminer service):

docker compose up

Conclusion

Hopefully, by documenting these steps I will not only be helping myself by not needing to search every time, and maybe this will also be useful for anyone else who stumbles upon this.