Containers have revolutionized the way software is built and shipped. They also provide a means wherein software no longer needs to be mandatorily installed since it can be directly run in the container. In this post, you will run PostgreSQL (postgres) and pgAdmin 4 in separate docker containers on a Windows machine using docker toolbox. Once both docker containers are up, you will use pgAdmin to connect to the postgres database.
This post assumes that the reader has
docker installed and running on a Windows machine. Installation instructions can be found in the official documentation.
PostgreSQL is a free and open-source relational database management system
pgAdmin 4 is an open-source database management tool for postgres
Creating the docker containers
To create the postgres and pgAdmin docker containers, we will use a
version: '3.2' services: postgres: image: postgres:latest ports: - '5432:5432' container_name: postgres environment: POSTGRES_USER: 'postgres' POSTGRES_PASSWORD: 'postgres-pass' POSTGRES_DB: 'postgres' volumes: - type: bind source: . target: /var/lib/postgres/data pgAdmin4: image: dpage/pgAdmin4 container_name: pgAdmin4 depends_on: - postgres ports: - '8080:80' environment: pgAdmin_DEFAULT_EMAIL: 'admin@pgAdmin.com' pgAdmin_DEFAULT_PASSWORD: 'pgAdmin-pass'
Let's go over the attributes in the file.
version is the version of the Compose file format
services provides configuration for the containers that will be started
image is the application image installed from Docker Hub
ports are the exposed host and container ports
environment contains the environment variables for the services defined in the
volume signifies the location of the persistent data stored in the container
To create the containers, you will need to run the
docker-compose -f docker-compose.yml up command. To verify if the docker containers are running, you can use the
docker container ls command.
Running pgAdmin 4
docker-compose.yml file specifies
8080 as the port to run pgAdmin. However, since docker is running on Windows, pgAdmin cannot be accessed using
http://localhost:8080. Instead, you will need to use the ip address of the virtual machine that is running docker toolbox. To get the ip address, you can run the
docker-machine ls command. The ip address, in this case, is
192.168.99.101. Therefore the url for pgAdmin will be
To log in, you will use the default email and password specified in the
Connecting to a postgres database
Once logged into pgAdmin, you will select the Add New Server option to connect to the postgres database.
The database name, username and the password will be as specified in the
yaml file. The hostname will once again be the ip address of the virtual machine.
Creating database objects with psql
To create database objects, we can either use pgAdmin or the
psql command line. To run
psql, you will first need to run a
bash shell in the container, then connect to our database and finally run our SQL statements. As an example, here is how you can create a new table.
Here is a link to official postgres tutorial.