postgres/README.md
2025-03-16 21:30:46 +01:00

99 lines
No EOL
2.4 KiB
Markdown

# PostgreSQL Docker Container
A Dockerized PostgreSQL database setup using Alpine Linux, designed for easy deployment and customization.
## Overview
This repository contains the necessary files to build a PostgreSQL Docker container. The container is pre-configured to initialize databases, users, and execute custom scripts on startup. It uses the lightweight Alpine Linux base image for efficiency.
## Prerequisites
- Docker installed on your system
- Access to the base image `git.esculta.es/aesculta/alpine-base` (or modify the `Dockerfile` to use a public base image)
## Getting Started
### Run the container
```bash
docker run -d \
--name my-postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_USER=customuser \
-e POSTGRES_DB=mydatabase \
-v postgres_data:/var/lib/postgresql/data \
-p 5432:5432 \
git.esculta.es/aesculta/postgres:latest
```
### Environment Variables
Configure the container using these environment variables:
* **POSTGRES_USER** (default: postgres): Database superuser name
* **POSTGRES_DB** (default: same as POSTGRES_USER): Default database name
* **POSTGRES_PASSWORD**: Password for the superuser (recommended for production)
**Warning:** If no password is set, the container will use trust authentication (insecure).
### Initialization Scripts
Place SQL or Shell scripts in `/docker-entrypoint-initdb.d` to execute them during initialization:
1. Create a files directory
2. Add your scripts:
* .sql files will be executed by psql
* .sh files will be sourced in the shell
* Rebuild the Docker image
Example structure:
```bash
/files
/docker-entrypoint-initdb.d
init.sql
setup.sh
```
### Persistent Data
Data is stored in the Docker volume mounted at /var/lib/postgresql/data. To persist data between container restarts:
```bash
# Create a named volume
docker volume create postgres_data
# Use the volume in your container
docker run ... -v postgres_data:/var/lib/postgresql/data ...
```
### Security Considerations
1. Always set POSTGRES_PASSWORD in production environments
2. The default configuration allows remote connections (`host all all 0.0.0.0/0`)
3. For production use:
* Consider customizing pg_hba.conf
* Use proper TLS certificates
* Restrict network access
### Exposing the Service
The container exposes PostgreSQL on port 5432. Map to host port using:
```bash
-p [HOST_PORT]:5432
```
### Build the Docker Image
```bash
docker build -t my-postgres .