Running Postgres and PgAdmin inside Docker containers

·

3 min read

Postgres is one of the most popular open-source relational databases available in the market today. PgAdmin is just a web-based tool which is used to perform data manipulating actions on Postgres databases. In this post, I'd discuss why it could be a better idea to use containers instead of having them installed in the system directly.

First, we would discuss Docker and containers. Some familiarity with Docker is expected to have a complete grasp of this post. Docker containers are packaged lightweight piece of software that is independent of the host system. Inside the Docker containers, you could have separate instances of Postgres installed on your system each independent of the others. Containers use images as blueprint to launch instances which can be configured. This allows us to use Postgres without worrying about the dependencies to be installed because, in Images, all these dependencies are already included. We would use Docker Compose which is a tool to handle multiple containers at once using a YAML script. I also assume that you have Docker with Compose installed on your system. We would have a 'docker-compose.yml' file which looks like this in our root folder.

version: '3.8'
services:
  db:
    container_name: pg_container
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: test_db
    ports:
      - "5432:5432"
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - "5050:80"

Here, we would spawn two containers for Postgres and PgAdmin respectively based on their official images available on the Docker Hub. We have given the default username and password for these applications in the YAML config file itself.

We have mapped port 5050 on our host system to port 80 of the PgAdmin container. We can access this PgAdmin instance from our host system through port 5050. We can log in using the default credentials provided, username: and password : root

You should now be inside the PgAdmin web interface running on port 5050. We'd now create a server which would connect the Postgres container with this PgAdmin container. Once, connected we'd be able to add databases and perform other operations through a graphical user interface. Create a new server, inside properties under the connection tab, you'd find database server settings to be configured. What's cool is that you can use the container_name you provided in the Postgres configuration inside the YAML file. Give the server_name as 'pg_container', and keep the username and password as 'root'. This you can observe from the YAML Docker-compose file we used. Click on "save" and you should have the PgAdmin connected with the Postgres running inside a container. You can now perform actions like adding/removing databases, users and much more.

Chances are that you'd like to connect this Postgres to one of the applications running on the host system. I've one sample code to connect for one web application I created in Fast-API.

SQLALCHEMY_DATABASE_URL = f"postgresql://root:root@127.0.0.1/fast-kanban"

This is the connection string I used to connect m web app to the database using SQLAlchemy. We have "username", "password" and "hostname" in sequence after "PostgreSQL" in the connection string.

That is all for this post. If you liked this article, consider following me on Hashnode for my latest publications. The comment section is all yours if you have anything to share on this topic.