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
Table of Contents
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:
- ACID Compliance: Ensures reliability with Atomicity, Consistency, Isolation, and Durability properties.
- Support for Advanced Data Types: Includes support for JSON, XML, and hstore (key-value pairs).
- Full-Text Search: Allows efficient searching of text data.
- 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.
- MVCC (Multiversion Concurrency Control): Allows multiple transactions to access the database concurrently without locking, enhancing performance.
- Scalability: Suitable for both small applications and large-scale enterprise solutions, supporting a variety of workloads.
- Extensibility: Users can add new types, operators, index types, and procedural languages.
- Replication and High Availability: Supports synchronous and asynchronous replication for high availability and disaster recovery.
- 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:
- Isolation: Containers provide a high level of isolation, ensuring that PostgreSQL runs in its own environment without interference from other applications.
- Portability: Docker containers can run on any system that supports Docker, making it easy to move PostgreSQL instances between development, testing, and production environments.
- Consistency: Ensures a consistent environment across different stages of development and deployment, reducing the “it works on my machine” problem.
- Simplified Management: Docker makes it easy to start, stop, and manage PostgreSQL instances with simple commands.
- Scalability: Containers can be quickly scaled up or down to meet changing demands.
- Resource Efficiency: Containers share the host system’s kernel and resources, leading to efficient utilization of system resources.
- Automation: Integration with CI/CD pipelines can streamline database deployment and testing processes.
Cons:
- Performance Overhead: Although minimal, there is some performance overhead compared to running PostgreSQL directly on the host system.
- 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.
- Networking Complexity: Container networking can add complexity, especially in multi-container setups where PostgreSQL needs to communicate with other services.
- Learning Curve: There may be a learning curve for those unfamiliar with Docker and containerization concepts.
- Resource Limits: Containers share the host system’s resources, which can lead to contention if not managed properly.
- Debugging Challenges: Debugging issues within a container can be more complex compared to a traditional setup.
- 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.
Read Also: How to install PostgreSQL on Ubuntu Server 18
Recommended Online Training: PostgreSQL Database Administration on Windows/Linux
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.
Login as
- User: ahmer@centlinux.com
- Password: 123
Click on Add New Server to add a PostgreSQL database server.
Provide connection settings as we have provided above and click on Save.
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.