Docker Compose and PostgreSQL DB in a Flash
This article shows you how to get a PostgreSQL DB running in 5 minutes and only 4 easy steps. This article has nothing to do with with Aaron Taylor-Johnson’s Quicksilver.
Im assuming the following:
- You have Docker installed
- You have Docker Compose installed
If you do not have these installed, check out these articles:
- Setting up Docker on Windows
- Installing docker and docker-compose on CentOS 7
Step 1: Create Directories
Run the following commands to create necessary directories
mkdir -m 777 -p ./data/postgresql/ ./data/db/ ./data/pgadmin/
Step 2: (Optional) Additional Configuration
There is no step two :)
Step 3: The Compose
Create a file called docker-compose.yml and copy and paste the following piece of code.
Run the following command to bring up the containers
docker-compose up -d
You should see the following:
Note:
This compose file includes two additional containers called adminer and pg-admin to help connect and view contents of the database
- The compose file forwards the default PostgreSQL port (5432) to itself
- Adminer is mapped to port 8088
- Pgadmin is on port 8080
Step 4: Connect to the Database
- Navigate to: http://localhost:8088/
- Login with the database details specified in the compose file in db section
- Once you login, you should see contents of your database as shown below, we got nothing.
- (Optional) You could also use pgadmin to connect to your db, Navigate to: http://localhost:8080/, and login with details specified in the compose file in pgadmin section
- Now you can connect your pgadmin instance by clicking add server as shown below:
- Give it a name and then click the connection tab to provide database details (specified in the compose file) as shown below:
- Please note that the host name is what you named the database service in the compose file — in this case: db.
- Now hit save and start interacting with you database. The pgadmin UI is much better than the adminer one.
This article is part of a series called:
“Devil’s in the Database: A Short Story of Docker and Databases”.
This series looks at how to use docker-compose to set up different database.