How to set up a PostgreSQL Database with Podman
Last Updated :
30 Aug, 2024
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
- 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
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
Step 4: Getting Started with Podman
Set up and Launch the Podman Machine:
podman machine init
podman machine start
Podman Connection Verification:
podman system connection list
- 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
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
Step 7: Enumerate Active Containers
podman ps
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.
Similar Reads
How to Setup a PostgreSQL Database Cluster
A PostgreSQL database cluster refers to a collection of databases managed by a single instance of the PostgreSQL server. Setting up a PostgreSQL cluster is an essential task for organizing multiple databases and achieving high availability, scalability, and load balancing. Whether we are working wit
5 min read
How to Export PostgreSQL Database Without Data Using SQL?
When we are working with the PostgreSQL database, there are multiple times we need to export the database structure. This approach is useful when we create a skeleton database or migrate the schema changes for different environments or systems. In this article, we will explore the process of exporti
3 min read
How to Migrate a MySQL Database to PostgreSQL using pgloader?
Database migration is a common task in software development when switching between different database management systems (DBMS). In this article, we'll explore how to migrate a MySQL database to PostgreSQL using a powerful tool called pgloader. We'll cover the concepts involved, and the steps requir
6 min read
How to Dump and Restore PostgreSQL Database?
PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
How to List Databases and Tables in PostgreSQL using PSQL
PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w
3 min read
How to Install PostgreSQL on a Mac with Homebrew
Homebrew is a popular manager that is used for the installation of different types of software in the Mac as well as Linux operating systems. we can use Homebrew for installing other software instead of running specific commands. This improves the security as compared to not using homebrew for the i
6 min read
How to Create a New User With Full Privileges in PostgreSQL?
PostgreSQL provides a way to give a user full privileges to do anything with the database. The database objects like schema, table, function, and so on. The 'GRANT' command is used in PostgreSQL to provide a user with any specific privileges or to override the role of the user. In this article, we a
6 min read
Run PostgreSQL on Docker and Setting Up pgAdmin
PostgreSQL, an effective tool, is a freeÂ-to-use relational database management system. Docker can quickly construct and orcheÂstrate its instances without bothering about the complexity of setup or depeÂndencies. This step-by-step simple guide will show you how to get PostgreÂSQL on Docker, and the
7 min read
How to Change a User to Superuser in PostgreSQL?
Changing a user to a superuser in PostgreSQL is a straightforward process that involves using commands to alter the user's role. PostgreSQL, as a robust open-source relational database management system, allows administrators to manage user roles efficiently. PostgreSQL provides the flexibility to a
5 min read
Sending data from a Flask app to PostgreSQL Database
A database is used to store and maintain persistent data that can be retrieved and manipulated efficiently. we usually need a database, an organized collection of data for example in an e-commerce web app where we need to store the data about the users, orders, carts, shipping, etc. In a database, d
6 min read