5 Creating new databases
Abdalrahman Alhindi edited this page 2026-04-17 12:38:02 +00:00

Database Setup with Docker

1- Creating multiple databases in the same Container:

Description

A single PostgreSQL container can host multiple databases. Additional databases are created during container initialization using SQL scripts.


Characteristics

  • One PostgreSQL instance
  • Multiple databases are created inside the same container
  • shared configuration (user, password, and port)

Advantages

  • Simple setup and configuration
  • Low resource usage. I.e., only one container
  • Easy to manage in development environments

Disadvantages

  • Single point of failure, where if the container crashes or stops, all databases are affected
  • Shared credentials and configuration
  • Less isolation between databases.

When to use:

  • Local development
  • small-scale setups, systems that are not comlex or large
  • when simplicity is preferred over isolation


2- Creating separate containers for each database

Description

Each database runs in its own PostgreSQL container, defined as separate services in compose.yaml


Characteristics

  • One database per container
  • Independent configuration per database
  • Separate storage, ports, and credentials

Advantages

  • Strong isolation between databases
  • No Single point of failure
  • More flexibility, can have different configs per database.

Disadvantages

  • Higher resource usage
  • More complex setup and maintenance
  • More Configuration required

When to use:

  • Larger systems
  • When databases must be isolated
  • When reliability and fault tolerance are important


Recommendation

For most development use cases, using the first option, multiple databases in a single container is efficient and simpler. However, for more advanced scenarios requiring isolation or higher reliability, the separate containers for each database solutions, is considered to be better.



How to

step-by-step guide can be found here