Skip to content

Instantly share code, notes, and snippets.

@tym-xqo
Last active July 1, 2021 17:10
Show Gist options
  • Save tym-xqo/b5208fe91e7625452d061761d2520c43 to your computer and use it in GitHub Desktop.
Save tym-xqo/b5208fe91e7625452d061761d2520c43 to your computer and use it in GitHub Desktop.
How to run Postgres locally in a Docker container

How to run Postgres locally in Docker

  • If you have a local Postgres service running on port 5432, stop it first
  • Get Docker Desktop for Mac if you don't have it already
  • Pull Postgres 10 image from Docker Hub: docker pull postgres:10
  • Create a directory in a known location that will be mounted as a data volume where the container will store the database files: mkdir -p -m 0700 ${HOME}/Library/postgres-data
  • Run the image (note that POSTGRES_PASSWORD is required in the container environment, and will be the password for the postgres user in the container Postgres instance):
     docker run -d \
       --name=dev-postgres \
       -e POSTGRES_PASSWORD=super-secret-password \
       -v ${HOME}/Library/postgres-data/:/var/lib/postgresql/data \
       -p5432:5432 \
        postgres:10
  • Check that the container is running: docker ps
    $ docker ps
     CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                    NAMES
     8fef9277aa85   postgres:10   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   0.0.0.0:5432->5432/tcp   dev-postgres
  • Check that you can connect to the database: psql -h localhost -p 5432 -U postgres -W postgres and enter the $POSTGRES_PASSWORD value when prompted
  • 🎉
@millerjs
Copy link

millerjs commented Apr 1, 2021

Note if apple silicon, then https://docs.docker.com/docker-for-mac/apple-m1/ 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment