Docker Postgres/Postgresql Multiple Database
In this article, We will explain you how to set up Postgresql following multiple databases using Docker-Compose
- root (use can use it for local development)
- test (use can use it for testing)
The production database environment. You can use cloud databases such as AWS RDS, Heroku, etc.
A dedicated environment for database for local development.
A dedicated environment for database testing.
Why we need multiple databases?
If you have only a Production Database and you are developing a new feature if something breaks it will affect the whole Production Database.
For best practice, You must have a different Database Development Environment because if you are developing a new feature in a Development Environment if something breaks it will not impact on Production Database Environment.
Let’s Start Creating Multiple Environment Database
Hope you are familiar with “Docker-Compose”
├── docker-compose.yml (File)
├── docker_postgres_init.sql (File)
Create a new file docker-compose.yml
Above file, we have created a Postgres Docker container with Port No: 5432
As well as set environment variables such as default username, password of Postgres container.
if you check the entire file we have also created volumes that point to the `docker_postgres_init.sql` file.
- ./docker_postgres_init.sql:/docker-entrypoint initdb.d/docker_postgres_init.sql
The script inside “docker_postgres_init.sql” will create multiple databases upon container startup. You can see the following file we have created 2 database and different 2 users for each database.
- root (Default root for local development)
- test (For Testing)
When you run the following command in the root directory for the project.
then you will see in the console docker_postgres_init.sql script will be executed as the following screenshot
that means your script executed successfully.
How to test connection
For testing, we used Pgadmin4 configured already in the docker-compose file.
Try to run following URL in the browser
and enter username and password as following screenshot.
after successful login, you will see the following screen.
Then click on to server -> create -> server
You will see the following pop up then in the General tab
Then click on Connection tab as the following screenshot then input Hostname
You can get Hostname by typing following command
$ ifconfig |grep inet
then click on to Save Button
then you will see the connection successful on the left side of the Pgadmin 4 navigation as shown below.
Follow the same steps for the Test user
Docker Compose Postgres Multiple Databases. Contribute to oxlb/docker-postgres-multiple-databases development by…
Thank you :)