PART 1 · THE POSTGRESQL REPRODUCIBLE CONTAINER
Procedure for Creating Tables and Auto-Initialization in Docker
How can you ensure that any developer or tester can start the finished database with a single command: `docker compose up -d`?
DDL Topological Order (from parents to children)
The /docker-entrypoint-initdb.d/ directory and its pitfall
PostgreSQL checks the target table when the REFERENCES clause is executed. Let's create them strictly by level:
1. Tables without foreign keys: clans, items, matches
2. First-level entities: players (references clans)
3. Join tables and facts: purchases, match_participants
4. Dependent logs: kill_log (references match_participants)
• The official postgres:16 image automatically runs all *.sql and *.sh files from /docker-entrypoint-initdb.d/ in alphabetical order.
• A crucial detail: initialization scripts run ONLY on the first startup with an empty volume!
• If you’ve modified init.sql, a regular restart won’t change anything—you’ll need to run `docker compose down -v`.
# docker-compose.yml (из стенда Lesson 5/dba/)
services:
postgres:
image: postgres:16
container_name: itmo_pg_dba
ports:
- "127.0.0.1:5435:5432" # Публикуем порт ТОЛЬКО на localhost!
volumes:
- pg_dba_data:/var/lib/postgresql/data
- ./live_admin_pg.sql:/docker-entrypoint-initdb.d/01_live_admin_pg.sql:ro
Tip for Windows + Git: Add *.sql text eol=lf to .gitattributes so that CRLF line breaks don't interfere with Linux containers.