Open In App

How to set up a PostgreSQL Database with Podman

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Podman is a tool that developers and system administrators are using more and more to manage and deploy their software stacks as the need for containerized applications grows. We will look at how to use Podman to set up and maintain a PostgreSQL database in this tutorial. The overview of PostgreSQL and Podman, the setup procedure, customization choices, backup and restore techniques, and PostgreSQL clustering for high availability will all be covered. By the time it's all through, you'll know exactly how to use Podman to set up, configure, and run a PostgreSQL database in a containerized setting.

This article explains how to use Podman to launch a simple standalone PostgreSQL database. This technique demonstrates how to establish a working database for development purposes using Podman and the PostgreSQL image.


What is PostgreSQL ?

PostgreSQL is a strong open source relational database management system that is distinctly regarded for its dependability and complex capabilities. It is able to handle tricky queries and custom facts types at the same time as adhering to SQL standards. Its scalability and versatility make it a famous choice for applications of all sizes.

What is Podman?

Podman is a container management tool that is similar to Docker, but it does not require root access. It is normally used to installation PostgreSQL because it permits for secure, non-root containers that are compatible with Docker commands and do not rely on a background services, making it less complicated to control.

Terminologies

  • PostgreSQL: PostgreSQL is a powerful open-source object-relational database system. It is known for its performance, feature robustness, and reliability.
  • Container: A container is a lightweight package that includes everything needed to run an application, such as code, runtime, system tools, and libraries.
  • Podman: A containerized engine that gives you system-wide control manipulate over how bins are run, maintained, and managed. It offers a characteristic set this is comparable to that of Docker.
  • Image: The design or model that was applied to build a container. It includes all the files, configuration, and dependencies required to operate an application.
  • Cluster: A collection of PostgreSQL instances collaborating to offer failover, load balancing, and high availability.
  • Standby Server: A PostgreSQL server that serves as a redundant and highly available data source by receiving and applying updates from the primary server.
  • Primary Server: The primary PostgreSQL server in charge of managing writes and data replication to backup servers.

Step-by-Step Guide to Setting Up PostgreSQL with Podman

Step1: Complete Setting Up Of Installation

  • Make sure Podman is set up on your computer. Using your operating system's package manager, you can install it.
winget install --id=RedHat.Podman -e
Install Podman
  • To accept the terms and proceed with the installation, just write Y and hit Enter.

Check the Installation of Podman

winget list | findstr Podman

If not then follow the following steps:

  • Include Podman in the Paths system.
  • Locate the directory for installation: Navigate to the directory (such as C:\Program Files\RedHat\Podman\) where Podman is installed.
  • Replicate the Route: Copy the directory's whole path
  • Put the folder in the PATH: Right-click in File Explorer or on the desktop to select This PC or My Computer.
  • Select Properties.
  • Select the Advanced System Settings option.
  • Press the button for Environment Variables.
  • Locate and choose the Path variable under the System variables section. Select Edit.
  • Paste the path you previously copied by clicking New.
  • To close each and every window, click OK.

Command Prompt Restart:

start cmd

Step 2: Checking the Podman Installation

podman --version
 To check the podman version

Step 3: Run the below command on Powershell to complete Setup

  • Search for Powershell into search bar and click on Run as Administrator.
  • Turn on WSL:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Turn on the Virtual Machine Platform.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Turn on the Virtual Machine Platform

Step 4: Getting Started with Podman

Set up and Launch the Podman Machine:

podman machine init
podman machine start
Setup and Launch the Podman Machine

Podman Connection Verification:

podman system connection list
Podman Connection Verification
  • The Podman machine appears to have been successfully initialized. It should now be ready for you to utilize.

Step 5: Check the Status of Podman Machine

podman machine ls
To check the status of Podman Machine

Step 6: Set Up PostgreSQL Container

podman pod create --name mypod -p 5432:5432
podman run -d --name mypostgres --pod mypod -e POSTGRES_PASSWORD=mysecretpassword postgres:latest
Testing a container

Step 7: Enumerate Active Containers

podman ps
To list the container

In this tutorial, we use Podman on Windows to set up a PostgreSQL database. You can now run operations on your PostgreSQL database as needed and maintain it.

Example:

Personalized PostgreSQL Container

You would possibly desire to alter the setup when the usage of PostgreSQL in a container based totally for your unique necessities. You can configure several elements of the PostgreSQL server by means of passing surroundings variables to the container the usage of Podman.

Using custom surroundings variables to launch a PostgreSQL box is demonstrated via the following instance:

podman run -d --name postgres \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=myapp \
-p 5432:5432 \
-v pgdata:/var/lib/postgresql/data \
postgres

In this scenario, we set the POSTGRES_DB variable to establish a database called myapp on container startup, and we set the POSTGRES_PASSWORD environment variable to mypassword. To further alter the server configuration, you can also mount a unique PostgreSQL configuration file (such as postgresql.conf) to the container.

PostgreSQL Clustering for High Availability

You can use Podman to configure a PostgreSQL cluster and guarantee maximum availability for your PostgreSQL deployment. To do this, multiple PostgreSQL instances must be running, with one running as the primary server and the others running as standby or replication servers.

So here will go through with one example below:- podman network create pgcluster

podman run -d --name postgres-primary \
--network pgcluster \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=myapp \
-v pgdata-primary:/var/lib/postgresql/data \
postgres

Begin with the standby PostgreSQL containers

podman run -d --name postgres-standby1 \
--network pgcluster \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=myapp \
-v pgdata-standby1:/var/lib/postgresql/data \
postgres \
postgres -c 'primary_conninfo='"'"'host=postgres-primary port=5432 user=postgres password=mypassword'"'"''
  • To add more standby containers, simply repeat the previous command, changing the volume name and container name as necessary.
  • The required replication and failover mechanisms must be configured. Programs such as pgbouncer and pgpool-II can be used for this.

Users can accomplish high availability, enhanced read scalability, and automatic failover in the event of a primary server failure by configuring a PostgreSQL cluster using Podman.

Conclusion

This article presents a thorough outline of the process for establishing and managing a PostgreSQL database using Podman, an effective containerization solution. Initially, it offers an introduction to Podman and PostgreSQL, followed by a detailed walkthrough of the steps involved in creating a PostgreSQL database, customizing the container, and managing backups. By utilizing Podman's containerization capabilities, users can enjoy the benefits of enhanced portability and scalability when working with PostgreSQL. This guide aims to provide individuals with the necessary knowledge and skills to effectively deploy and manage robust, scalable applications based on PostgreSQL in a containerized environment.






Next Article
Article Tags :

Similar Reads