Page:
How to- Creating Databases in Docker
Pages
API SMHI
API TimeEdit
Accessibility Testing
Api documentation
Bat/Ps
Branch Creation
C# Coding Convention
CSS Coding Convention
Coding Conventions
Creating new databases
Dapper
Documentation for service endpoints
Documentation of useTemperature and mock sensor
Energy endpoints
Environment
Evaluation react-grid-layout
Evaluation Localization
Evaluation Password hashing
Evaluation Testing framework
Evaluation of Logging Frameworks
Formatter
Frontend model
Generic Diagram for Sensor data flow
Home
How to- Creating Databases in Docker
Implementing different Styles
Implementing new Mockup sensors
Implementing new Views
Installation script documentation
Installation
Issues
JSON Coding Convention
Jest Testing
Layout endpoints
Linux
Localization
Login endpoints
Mac
Middleware model
Mock API Ceation Guidelines
Mockup models
Model Tools and Standards
Parking endpoints
React/JavaScript Coding Convention
React
Reviews
Room booking endpoints
SQL Coding Convention
Scheduled Database tasks
Scripting Coding Convention
Secrets and .env
Server Access
Server-Deployment
Service/API models
System models
System requirements
Temperature endpoints
Testing API with Bruno
Testing Architecture
Testing frontend for Mobile devices
Testing frontend with Playwright
Testing methodology
Västtrafik API
Webpage Design
Windows
pg_cron and scheduled database jobs
xUnit Testing
No results
1
How to- Creating Databases in Docker
Abdalrahman Alhindi edited this page 2026-04-17 12:33:21 +00:00
Database Setup with Docker
1- Creating multiple databases in the same Container:
When a PostgreSQL container is started for the first time, all .sql and .sh files located in
/docker-entrypoint-initdb.d
are automatically executed.
How to use it:
Create a .sql file (or use "docker_postgres_init.sql") containing the databases needed, ex:
CREATE DATABASE db_init;
CREATE DATABASE sensor_test;
Then mount the file in compose.yml (Note: the docker_postgres_init.sql file is already mounted, so using it is easier.) as such:
postgres:
volumes:
- ./docker_postgres_init.sql:/docker-entrypoint-initdb.d/docker_postgres_init.sql
Important notes:
- The default database defined by
POSTGRES_DB: sensorswill still be created automatically. - Init scripts only run on first container initialization. If new databases are added later, make sure to reset the volume. In the terminal, type the following in the same order:
docker compose down -v
docker compose up --build
- All databases in the same container will share the same PostgreSQL instance, use the same user and password, and use the same port.
Creating separate containers for each database
Another approach is to create a separate PostgreSQL container for each database by adding additional services in compose.yml.
To use
Add another service using the postgres image as such:
postgres_test:
image: postgres
container_name: PostgreSQL
#ports:
- "5432:5432"
environment:
POSTGRES_DB: sensors_test
POSTGRES_PASSWORD: someOtherPasword123
networks:
- DTnet
restart: unless-stopped
volumes:
- pgdata_test:/var/lib/postgresql
Considerations
- Each container must have a unique container name, port mapping, and its own volume.
- You can configure different database names, different users/passwords, and separate storage.
1. Setup
2. Standards
- Coding Conventions
- Issues
- Branch creation
- Reviews
- Implementation Standards
- [WIP] Creating new databases
- Localization
3. Models and Diagrams
4. Testing
5. Documentation
- Documentation for service endpoints
- API documentation
- Webpage Design
- Secrets and .env
- Evaluations
- Installation and Rebuild script documentation
6. Guides and How-to's
7. Micro Service Mockup Api
- Guidelines Mircro Service Mockup
- Documentation of useTemperature/useTemperatureTimeSeries mockup sensor