/postgres-docker-template

A Docker-based PostgreSQL setup with pgAdmin and automated migrations

Primary LanguageMakefileMIT LicenseMIT

Docker PostgreSQL Template

Welcome to the Docker PostgreSQL template repository! This guide will help you quickly set up a PostgreSQL environment using Docker, complete with a migration tool and an administrative interface. Here's how to get started, along with some essential information about managing your database containers.

Prerequisites

To use this template, you will need:

  • Docker: Ensure you have Docker installed on your system to run containers.
  • Make: This project uses make commands for simplifying operations.

Getting Started

Follow these steps to start your PostgreSQL services:

  1. Launch Containers

To start all services, simply run the following command:

make start

This command will first check if the necessary configuration files like .env exist. If not, it will automatically initiate the configuration process. During this process, you will be prompted to input the database name, user name, and password. Default values will be used if no input is provided. Once the configuration is set, Docker Compose will start all services.

This streamlined approach ensures that everything is set up and running with a single command.

Containers in Operation

This setup includes several containers, which are orchestrated to work together:

  • PostgreSQL: The main database server.
  • Migration Service (go-migrate): Automatically starts when PostgreSQL is ready, to handle database migrations.
  • pgAdmin 5: A web-based PostgreSQL administration tool, also starts after PostgreSQL is ready.

Available Commands

Here are some useful commands provided by this project:

  • make start(default target): Starts all containers.
  • make stop: Stops and removes all containers.
  • make init: Generates all required files for the setup.
  • make clean: Removes all generated files.
  • make build: Rebuilds the Docker containers and starts them. This command is particularly useful for executing migration scripts. Since migration scripts are copied into the container at build time, a rebuild is necessary to execute newly added migrations.

Configuration

You can customize the setup by modifying the .env file generated by the make command. This file allows you to adjust various settings related to the containers themselves, such as:

  • Port Numbers: Specify the ports on which services will listen, ensuring they do not conflict with other applications.
  • Database Locale Settings: Configure regional settings that affect data sorting, character classification, and more.

These settings are just a few examples. The .env file contains multiple configurable variables that help tailor the Docker environment to fit your specific needs, enhancing both functionality and integration with existing systems.

The migration SQL files can also be changed. Please follow the naming convention specified by go-migrate.

Security

The configuration uses Docker Compose secrets to manage sensitive information like database names, usernames, and passwords securely. By default, the password is a randomly generated 13-character string. You can change the password length by editing the corresponding variable in the Makefile.