DEV Community

Cover image for Postgres up and running in less than 3 minutes with docker-compose πŸš€
Raphael MANSUY
Raphael MANSUY

Posted on β€’ Edited on β€’ Originally published at elitizon.com

16 2

Postgres up and running in less than 3 minutes with docker-compose πŸš€

Run Postgres using "docker-compose"

Create a local directory called "postgres"

$ mkdir postgres
$ cd ./postgres
Enter fullscreen mode Exit fullscreen mode

Create a "pgdata" directory inside "postgres" directory

$ cd ./postgres
$ mkdir ./pgdata
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ As a docker container is "stateless" we must create the directory "pgdata" to keep the data when the container is shut down.

Create a file called "docker-compose.yml" in "postgres" directory

$ touch ./docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

πŸ“„ docker-compose.yml file

version: "3.8"
services:
  db:
    image: "postgres:13"
    ports:
      - "5432:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=dbuser
      - POSTGRES_PASSWORD=admin2021
      - POSTGRES_DB=todoapp
Enter fullscreen mode Exit fullscreen mode
  • This file creates a host called "db" from a Postgres version 13 image. The TCP port 5432 (postgres) of the host "db" is exposed externally as TCP port 5432.

  • The local directory "./pgdata" is mapped as "/var/lib/postprogressql/data" inside the "db" host

  • The username, password and database name are exposed as an environment variable

Run the container in detached mode

$ docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

List of running docker containers

$ docker-compose ps
Enter fullscreen mode Exit fullscreen mode

Run command inside the container

$ docker-compose run db bash
Enter fullscreen mode Exit fullscreen mode

Connect postgres inside the host

$ psql  --host=db --username=dbuser --dbname=todoapp
Enter fullscreen mode Exit fullscreen mode

Connect to "postgres" from outside

$ psql --host=localhost --username=dbuser --dbname=todoapp
Enter fullscreen mode Exit fullscreen mode

stop container

$ docker-compose down
Enter fullscreen mode Exit fullscreen mode

Et voilΓ  πŸŽ‰ !

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (3)

Collapse
 
kotrotko profile image
kotrotko β€’ β€’ Edited

Thank you so much, it is very useful for newbies like me) At the same time, if I try

psql --host=db --username=dbuser --dbname=todoapp

, I get

psql: error: could not translate host name "db" to address: nodename nor servname provided, or not known

. Other commands work as expected. What does it mean and how to fix it?

Collapse
 
nsmorisset profile image
nsmorisset β€’

Hi,
For me it's ok with:
psql --host=database --username=dbuser --dbname=todoapp
but I do not know why

Collapse
 
timbogdanov profile image
Tim Bogdanov β€’

question:
my set up is very similar, when booting up the container, and logining into psql with username db and host it create a different ip address for the database container every time, in the env of my project id have to constently change this. How could I hardcode localhost in the postgres docker-compose.yaml file, so that when ever i boot up the containers the db container will always host at 127.0.0.1?

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay