Skip to content

How to start PostgreSQL database using Docker?

Published: at 05:06 AM

Getting started with PostgreSQL database with Docker

PostgreSQL is an open-source relational database management system similar to SQL database.

Requirements

  1. Please make sure docker is installed and the docker daemon process is running
  2. Must have a reliable and fast internet connection

Steps

Create a docker-compose.yml file. And paste the below content in that file.

version: '3.8'

services:
  db:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres

    ports:
      - 5432:5432

To start the PostgreSQL database, run the following command.

$ docker compose up

If you’re in an older version, then try running the following command

$ docker-compose up

Connect to PostgreSQL database

The database is currently running the default port 5432. The PostgreSQL database URL string syntax looks as follows,

postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA

For the above example, the connection string looks like below

postgresql://postgres:postgres@localhost:5432/postgres?schema=public

To stop the PostgresSQL container, either ctrl+cin the terminal or in the same path run the following command in the terminal to stop the container.

$ docker compose down