Deploy PostgreSQL Database on Podman: A Step-by-Step Guide

Deploy PostgreSQL on Podman

Learn how to deploy PostgreSQL database on Podman with this step-by-step guide. From installation to database setup, find out everything you need to know to run PostgreSQL containers efficiently.

Table of Contents

🔈Introduction

In the world of modern application development and containerization, Podman has gained significant traction as a container management tool. It’s known for being a lightweight, daemon-less alternative to Docker, providing developers with an efficient platform for running containers.

If you’re looking to deploy a PostgreSQL database on Podman, you’ve come to the right place! This blog will guide you through the entire process, from setting up Podman on your system to configuring PostgreSQL as a containerized service.

Let’s dive into the details of how you can deploy and manage PostgreSQL databases using Podman, with a focus on ensuring smooth performance, scalability, and security.


🤔What is Podman?

Podman is an open-source container engine that allows you to build, manage, and run containers on Linux, Windows, and macOS. One of the main benefits of Podman is that it doesn’t require a central daemon like Docker. Each Podman container is independent, and you can use Podman without needing root privileges, making it a secure and flexible option for container management.

Unlike Docker, which requires a background daemon (the Docker Engine), Podman is designed with a rootless architecture that offers better security. Podman works seamlessly with the same command line interface (CLI) as Docker, making it easier for users familiar with Docker to switch over.


Why Use Podman for PostgreSQL?

Before we dive into the actual deployment steps, let’s quickly examine why you might choose Podman for deploying PostgreSQL:

  • Daemon-less Architecture: Podman runs containers without the need for a long-running daemon. This can lead to reduced resource consumption and increased security.
  • Rootless Containers: Podman allows users to run containers without requiring root access, reducing the security risks associated with running containers as root.
  • Compatibility with Docker: Podman supports the same CLI commands as Docker. This makes it easy for developers familiar with Docker to transition seamlessly to Podman.
  • Performance: Podman containers can be more efficient in terms of memory and CPU usage, making it ideal for lightweight, scalable applications.
  • Enhanced Security: Podman integrates well with SELinux and other security policies, making it an excellent choice for production environments.

Step 1: Installing Podman

Before you can deploy PostgreSQL on Podman, you need to install Podman on your system. Here’s how to do it on popular Linux distributions:

For Ubuntu/Debian:

				
					sudo apt update && sudo apt install -y podman
				
			

For CentOS/RHEL:

				
					sudo yum install -y podman    # Older RHEL versions
				
			
				
					sudo dnf install -y podman    # RHEL/RPM-based flavors 8+ versions or higher
				
			

For Fedora:

				
					sudo dnf install -y podman
				
			

For macOS:

Install Podman using Homebrew:

				
					brew install podman
				
			

For Windows:

Windows users can install Podman via the Windows Subsystem for Linux (WSL) or by using the Podman Desktop application. Once you’ve installed Podman, verify the installation by running:

				
					podman --version
				
			

This should display the version of Podman installed on your system.

Step 2: Pull the PostgreSQL Container Image

Now that Podman is installed, let’s pull the official PostgreSQL image from the container registry. PostgreSQL’s official Docker image is compatible with Podman, so you can use the following command to pull the image:

				
					podman pull postgres
				
			

Select Docker unless you have a Red Hat Account:

				
					? Please select an image: 
    registry.access.redhat.com/postgres:latest
    registry.redhat.io/postgres:latest
  â–¸ docker.io/library/postgres:latest
				
			

This command will pull the latest official PostgreSQL container image, which is the most stable and secure version available.

Deploy PostgreSQL on Podman

Photo by admingeek from Infotechys

Understanding the PostgreSQL Image

The PostgreSQL container image comes pre-configured with the PostgreSQL database engine and some basic configurations. This image is customizable through environment variables to set the database name, user, and password, among other settings.


Step 3: Configuring PostgreSQL with Podman

To configure PostgreSQL in your Podman container, you need to set up a few essential environment variables. These include:

  • POSTGRES_DB: Name of the database to create.
  • POSTGRES_USER: Name of the user for the database.
  • POSTGRES_PASSWORD: Password for the user.

Here’s an example command to start a PostgreSQL container with basic configuration:

				
					podman run -d \
  --name postgres-db \
  -e POSTGRES_DB=mydb \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  -p 5432:5432 \
  postgres
				
			

Breakdown of the Command:

  • -d: Runs the container in detached mode (in the background).
  • --name postgres-db: Names the container “postgres-db.”
  • -e POSTGRES_DB=mydb: Creates a database named “mydb.
  • -e POSTGRES_USER=myuser: Sets the database user to “myuser.
  • -e POSTGRES_PASSWORD=mypassword: Sets the password for the user “mypassword.
  • -p 5432:5432: Maps the container’s PostgreSQL port (5432) to your system’s port 5432.

After running the command, Podman will start the PostgreSQL container, and you can access it on your local machine at localhost:5432.


Step 4: Connecting to PostgreSQL

Now that your PostgreSQL container is running, you can connect to it from your local machine or any other client. You can use psql, PostgreSQL’s command-line interface, or any GUI tool like pgAdmin.

Example: Using psql to Connect

If you have PostgreSQL installed locally, you can connect to the container like so:

				
					psql -h localhost -U myuser -d mydb
				
			

You’ll be prompted to enter the password for myuser. Once logged in, you can execute SQL queries just like you would on any PostgreSQL instance.


Step 5: Managing PostgreSQL Data in Podman

One of the most crucial aspects of running databases in containers is data persistence. By default, data in a container is ephemeral, meaning it will be lost once the container is removed. To ensure that your PostgreSQL data persists, you need to mount a host directory or a volume to store the database files.

Here’s an updated command to start the PostgreSQL container with persistent storage:

				
					podman run -d \
  --name postgres-db \
  -e POSTGRES_DB=mydb \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  -p 5432:5432 \
  -v /path/to/local/storage:/var/lib/postgresql/data \
  postgres
				
			
  • -v /path/to/local/storage:/var/lib/postgresql/data: Mounts the /path/to/local/storage directory on your host machine to the PostgreSQL data directory in the container.

By doing this, you ensure that even if the container is removed, your database data remains intact on your host system.


Step 6: Scaling PostgreSQL with Podman

If you need to scale PostgreSQL to handle more traffic or perform more complex operations, you can deploy multiple PostgreSQL instances or run additional services like load balancers or replication.

Podman supports multi-container setups through Pods, which allow you to group related containers together. You can create a Pod with PostgreSQL and an additional service like a replication container or a load balancer.

				
					podman pod create --name pg-pod -p 5432:5432
				
			
				
					podman run -d --pod pg-pod -e POSTGRES_DB=mydb -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword postgres
				
			

This command creates a Pod called pg-pod and runs the PostgreSQL container within it. You can add more containers to the Pod as needed.


Step 7: Monitoring and Logging PostgreSQL Containers

To ensure that your PostgreSQL database is running smoothly, you can use Podman’s logging and monitoring features.

Viewing Container Logs:

You can view the logs of a running container with the following command:

				
					podman logs postgres-db
				
			

This will show you the output logs from the PostgreSQL container, which can be helpful for debugging or monitoring the database.

Viewing Container Stats:

To view the resource usage statistics of your containers, run:

				
					podman stats postgres-db
				
			

This provides real-time stats on memory, CPU usage, and network IO.


📌Conclusion

Deploying PostgreSQL on Podman is a great way to leverage the benefits of containerization while maintaining control over your database environment. Podman’s lightweight, daemon-less architecture, combined with the power of PostgreSQL, gives you a robust solution for managing your data.

With this guide, you now have a clear understanding of how to:

  •  Install and set up Podman
  • Deploy and configure PostgreSQL containers.
  • Handle data persistence and container scaling.
  • Monitor your PostgreSQL containers.

By following these steps, you can ensure that your PostgreSQL database runs securely and efficiently in a containerized environment, making it easier to manage and scale as your application grows.


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *