Docker
PostgreSQL
Database Setup
Scripting
Automation

How to create User/Database in script for Docker Postgres

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Creating a user or database in a PostgreSQL container running with Docker can streamline development, testing, and production setups. Docker allows for easy management and deployment of containerized applications, and PostgreSQL is a prevalent choice for reliable and robust database management. Below is a comprehensive guide to creating a user and database via scripting within a Dockerized PostgreSQL instance.

Understanding Docker and PostgreSQL Integration

Docker containers allow services to be run consistently across different computing environments. PostgreSQL, an advanced relational database, is commonly used for handling data securely.

Docker-PostgreSQL Image

The postgres Docker image provides a way to quickly launch a PostgreSQL database. By specifying environment variables, we can automate the database setup, including user and database creation.

Docker Image Configuration

The official Postgres image comes with several configurable environment variables:

  • POSTGRES_USER: Specifies the username of the superuser.
  • POSTGRES_PASSWORD: Sets the password for the superuser.
  • POSTGRES_DB: Creates a database with this name.

Given these capabilities, we can programmatically create additional users or databases as needed through .sql scripts or custom Dockerfiles.

Creating a Script for User/Database Creation

While the basic setup can handle a single user and database, creating a script enhances reproducibility for more complex setups.

Step1: Create a Dockerfile

A Dockerfile is essential for building a custom image capable of running our script and setting up additional database parameters more complicated than the initial image supports.

dockerfile
1# Use the official postgres image from the Docker Hub
2FROM postgres:13
3
4# Add SQL script to create additional users/databases
5COPY ./setup.sql /docker-entrypoint-initdb.d/

Step 2: SQL Setup Script

The script performs SQL commands during the container initialization phase.

sql
1-- setup.sql
2CREATE USER newuser WITH PASSWORD 'password';
3CREATE DATABASE newdatabase;
4GRANT ALL PRIVILEGES ON DATABASE newdatabase TO newuser;

Step 3: Docker Compose File

A docker-compose.yml file enables easy configuration and management of multi-container applications. Utilize it to manage environment variables, volumes, and networks:

yaml
1version: '3.8'
2services:
3  db:
4    build: .
5    container_name: postgres_container
6    environment:
7      - POSTGRES_USER=admin
8      - POSTGRES_PASSWORD=admin_password
9      - POSTGRES_DB=maindb
10    volumes:
11      - db_data:/var/lib/postgresql/data
12volumes:
13  db_data:

Build and Run the Configuration

bash
1# Build the Docker image
2docker-compose build
3
4# Run the Docker container
5docker-compose up -d

Key Points Summary

StepDescription
DockerfileBuild a custom Postgres image including an auto-init script.
SQL ScriptSQL commands for user and database creation.
Docker ComposeProvides orchestration for flexible service management with environment variables.

Additional Considerations

  • Security: Always handle sensitive information like passwords through secure environment variables or secret management systems.
  • Persistence: Ensure data persists across container restarts using Docker Volumes.
  • Customization: Extend scripts to add more database objects, such as tables and indexes.

Conclusion

Automating the creation of users and databases in Dockerized PostgreSQL environments not only fosters more efficient workflows but also ensures consistency across development and production environments. Leveraging Docker, Docker Compose, and PostgreSQL scripts together offers a powerful mechanism for standardized application development.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.