Quickly Spin Up PostgreSQL With Docker Compose

Quickly Spin Up PostgreSQL With Docker Compose

Learn How to Install PostgreSQL Using Docker Compose

In this tutorial, we'll learn how to install PostgreSQL using Docker-Compose. By using Docker-Compose, we can configure an image the way we want and can turn the image on and of as needed.

Setup

Create a new directory for your PostgreSQL project. Inside of this directory, create a data directory and a docker-compose.yml file.

Project_Folder_For_Database
|---data
|___docker-compose.yml

Inside of the docker-compose.yml file paste this:

version: '3.7'
services: 
    db:
      container_name: docker-local-postgres
      image: postgres
      volumes:
        - "./data:/var/lib/postgresql/data"
      restart: always
      ports:
        - 5432:5432
      environment:
        POSTGRES_PASSWORD: password

Start the Container

To start the container run this command:

 docker-compose up -d

Stop the Container

To stop the container run this command:

 docker-compose down
 

Persisting Data

Whenever PostgreSQL modifies any data it will be persist locally inside of your data directory.

Run Commands Inside of the Container

To run Bash commands run this command:

docker-compose exec db bash

This will give you shell access to the container.

Conclusion

In this short tutorial, we'll learn how to spin up PostgreSQL with Docker-Compose and how to get shell access to the container.

Did you like it? Why don't you try also...

Docker MySQL, PostgreSQL, and MongoDB

Docker MySQL, PostgreSQL, and MongoDB

Learn how to use Docker to install MySQL, PostgreSQL, and MongoDB

Quickly Spin Up MongoDB With Docker Compose

Quickly Spin Up MongoDB With Docker Compose

Learn How to Install MongoDB Using Docker Compose

Quickly Spin Up MariaDB With Docker Compose

Quickly Spin Up MariaDB With Docker Compose

Learn How to Install MariaDB Using Docker Compose