Share on Social Media

Learn how to run a Postgres Docker container with our step-by-step guide. Simplify your database management with Docker’s ease of use and portability. #centlinux #postgres #docker

What is Postgres?

PostgreSQL, often referred to as Postgres, is a powerful and versatile open-source relational database management system (RDBMS). It was developed with a focus on extensibility and standards compliance.

Key Features of PostgreSQL:

  1. ACID Compliance: Ensures reliability with Atomicity, Consistency, Isolation, and Durability properties.
  2. Support for Advanced Data Types: Includes support for JSON, XML, and hstore (key-value pairs).
  3. Full-Text Search: Allows efficient searching of text data.
  4. Custom Functions and Triggers: Enables users to define their own functions, which can be written in various programming languages, such as PL/pgSQL, PL/Python, and PL/Perl.
  5. MVCC (Multiversion Concurrency Control): Allows multiple transactions to access the database concurrently without locking, enhancing performance.
  6. Scalability: Suitable for both small applications and large-scale enterprise solutions, supporting a variety of workloads.
  7. Extensibility: Users can add new types, operators, index types, and procedural languages.
  8. Replication and High Availability: Supports synchronous and asynchronous replication for high availability and disaster recovery.
  9. Robust Security Features: Includes SSL for secure connections, various authentication methods, and row-level security.

Use Cases:

  • Web Development: Frequently used as a backend database for web applications.
  • Data Warehousing: Capable of handling large amounts of data for analytical processing.
  • Geospatial Data Management: Supports the PostGIS extension for geographic objects, making it a popular choice for geographic information systems (GIS).
  • Enterprise Applications: Trusted by many large organizations for its reliability and performance.

Community and Support:

PostgreSQL has a strong, active community that contributes to its continuous improvement and provides extensive documentation and support. It also has a variety of third-party tools and extensions that enhance its capabilities, ensuring it remains at the forefront of database technology.

If you are new to Docker platform, then you should read Docker in Action (PAID LINK) by Manning Publications before moving forward with this article.

Pros and Cons of Running Postgres Docker Container

Running PostgreSQL in a Docker container has several advantages and disadvantages. Here are the key pros and cons:

Pros:

  1. Isolation: Containers provide a high level of isolation, ensuring that PostgreSQL runs in its own environment without interference from other applications.
  2. Portability: Docker containers can run on any system that supports Docker, making it easy to move PostgreSQL instances between development, testing, and production environments.
  3. Consistency: Ensures a consistent environment across different stages of development and deployment, reducing the “it works on my machine” problem.
  4. Simplified Management: Docker makes it easy to start, stop, and manage PostgreSQL instances with simple commands.
  5. Scalability: Containers can be quickly scaled up or down to meet changing demands.
  6. Resource Efficiency: Containers share the host system’s kernel and resources, leading to efficient utilization of system resources.
  7. Automation: Integration with CI/CD pipelines can streamline database deployment and testing processes.

Cons:

  1. Performance Overhead: Although minimal, there is some performance overhead compared to running PostgreSQL directly on the host system.
  2. Data Persistence: Proper configuration is needed to ensure data persists across container restarts and updates. This typically involves using Docker volumes or external storage solutions.
  3. Networking Complexity: Container networking can add complexity, especially in multi-container setups where PostgreSQL needs to communicate with other services.
  4. Learning Curve: There may be a learning curve for those unfamiliar with Docker and containerization concepts.
  5. Resource Limits: Containers share the host system’s resources, which can lead to contention if not managed properly.
  6. Debugging Challenges: Debugging issues within a container can be more complex compared to a traditional setup.
  7. Compatibility: Some extensions or tools may not work seamlessly in a containerized environment.

In summary, running PostgreSQL in a Docker container offers significant benefits in terms of isolation, portability, and management but requires careful consideration of data persistence, performance, and potential complexity in networking and debugging.

Recommended Online Training: PostgreSQL Database Administration on Windows/Linux

4006704 72fb 9show?id=oLRJ54lcVEg&offerid=1606991.4006704&bids=1606991

Docker Host Specification

We are using a Ubuntu Server based Docker Host with following specification.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Ubuntu Server 18.04 LTS
  • Hostname – docker-01.centlinux.com
  • IP Address – 192.168.116.218 /24

We are using a pre-configured Docker host in this article. For setting up the required environment on Red Hat based Linux server, please follow our previous article to install Docker on CentOS.

How to Run Postgres Docker Container?

Connect with docker-01.centlinux.com as a privileged user by using a ssh tool like PuTTY.

Create a directory to store configuration and data files related to PostgreSQL Docker container.

$ mkdir ~/postgres-01
$ cd postgres-01

Create a directory for PostgreSQL data files.

$ mkdir postgres_data

Create a docker-compose.yml file.

$ vi docker-compose.yml

And define postgres service therein.

version: "3.1"
services:
  db:
    image: "postgres:11"
    container_name: "postgres-01.centlinux.com"
    ports:
      - "5432:5432"
 environment:
      POSTGRES_PASSWORD: "123"
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
 links:
   - "pgadmin"

Pull the required postgres image from Docker Hub.

$ sudo docker image pull postgres:11
11: Pulling from library/postgres
...
Status: Downloaded newer image for postgres:11

Starting postgres container using our docker-compose.yml file.

$ sudo docker-compose up
[sudo] password for ahmer:
Creating postgres-01.centlinux.com ... done
Attaching to postgres-01.centlinux.com
...
postgres-01.centlinux.com | 2020-03-04 18:48:31.371 UTC [1] LOG:  database system is ready to accept connections

Open a new ssh session and connect with postgres container.

$ sudo docker exec -it postgres-01.centlinux.com bash
root@7bb0d4f1e4a6:/# su - postgres
postgres@7bb0d4f1e4a6:~$ psql
psql (11.7 (Debian 11.7-2.pgdg90+1))
Type "help" for help.

postgres=# exit
postgres@7bb0d4f1e4a6:~$ exit
logout
root@7bb0d4f1e4a6:/# exit
exit

PostgreSQL docker container is successfully configured.

How to Run pgAdmin4 Docker Container?

Pull the pgAdmin4 Docker image from Docker Hub.

$ sudo docker pull dpage/pgadmin4
Using default tag: latest
latest: Pulling from dpage/pgadmin4
...
Status: Downloaded newer image for dpage/pgadmin4:latest

Edit docker-compose.yml file and add pgadmin service.

$ vi docker-compose.yml

Now define pgadmin service under the services section.

 pgadmin:
    image: "dpage/pgadmin4"
    container_name: "pgadmin4.centlinux.com"
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: "ahmer@centlinux.com"
      PGADMIN_DEFAULT_PASSWORD: "123"

Allow the pgAdmin service port in Ubuntu firewall on Docker Host.

$ sudo ufw allow 5050/tcp
[sudo] password for ahmer:
Rule added
Rule added (v6)

Start PostgreSQL and pgAdmin4 Docker containers using docker-compose command.

$ sudo docker-compose up
Starting pgadmin4.centlinux.com ... done
Starting postgres-01.centlinux.com ... done
Attaching to pgadmin4.centlinux.com, postgres-01.centlinux.com
...
pgadmin4.centlinux.com | [2020-03-05 14:56:19 +0000] [81] [INFO] Booting worker with pid: 81

Test PostgreSQL and pgAdmin Containers

Open URL http://docker-01.centlinux.com:5050 in a web browser.

pgAdmin4 Login
pgAdmin4 Login

Login as

  • User: ahmer@centlinux.com
  • Password: 123
pgAdmin4 Dashboard
pgAdmin4 Dashboard

Click on Add New Server to add a PostgreSQL database server.

pgAdmin4 - Create Server 1
pgAdmin4 – Create Server 1
pgAdmin4 - Create Server 2
pgAdmin4 – Create Server 2

Provide connection settings as we have provided above and click on Save.

pgAdmin4 Dashboard 2
pgAdmin4 Dashboard 2

Our PostgreSQL database server has been added in pgAdmin4 docker container.

Final Thoughts

Running PostgreSQL in a Docker container is a powerful approach that combines the robustness of PostgreSQL with the flexibility and convenience of Docker. It offers numerous benefits, including consistent environments, simplified management, and scalability. However, it also comes with challenges like ensuring data persistence and handling potential performance overhead. By understanding both the advantages and the potential drawbacks, you can effectively utilize Docker containers to manage your PostgreSQL databases.

If you’re looking for expert guidance on how to run a PostgreSQL Docker container, I offer professional services to help you set up, configure, and optimize your PostgreSQL environment. Check out my Fiverr profile for more details on my services and how I can assist you in achieving a seamless and efficient PostgreSQL setup.

Leave a Reply