1080*80 ad

Step-by-Step Deployment of PostgreSQL Database on Podman

Deploying a PostgreSQL database instance using Podman offers a clean, isolated, and portable solution for managing your database infrastructure. This step-by-step guide will walk you through the process, ensuring your data is persistent and accessible.

Prerequisites:

Ensure you have Podman installed and configured on your system.

Step 1: Pull the PostgreSQL Image

First, you need the PostgreSQL container image. Pull the official image from a container registry (like Docker Hub, which Podman can access by default). Open your terminal and run:

podman pull postgres

This command fetches the latest version of the PostgreSQL image. You can specify a specific version by adding a tag, like postgres:14.

Step 2: Create a Persistent Volume

Databases require data to persist beyond the life of a container. We use a Podman volume for this. Create a named volume:

podman volume create pgdata

Replace pgdata with a name you prefer for your volume. This volume will store the actual database files.

Step 3: Run the PostgreSQL Container

Now, run the container, mounting the persistent volume and setting necessary environment variables.

podman run –name my-postgres \
-e POSTGRESPASSWORD=yourstrong_password \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
-d postgres

Let’s break down this command:

  • –name my-postgres: Assigns a recognizable name to your container.
  • -e POSTGRESPASSWORD=yourstrong_password: Sets the password for the default postgres user. Replace your_strong_password with a secure password. This is a mandatory environment variable for the official image.
  • -v pgdata:/var/lib/postgresql/data: Mounts the named volume pgdata to the default data directory inside the container (/var/lib/postgresql/data). This ensures data persistence.
  • -p 5432:5432: Maps the default PostgreSQL port (5432) inside the container to port 5432 on your host machine. You can change the host port if needed (e.g., -p 6432:5432).
  • -d: Runs the container in detached mode, meaning it runs in the background.
  • postgres: The name of the image to use.

Step 4: Verify the Container is Running

Check if your container started successfully:

podman ps

You should see a container named my-postgres listed with a status indicating it’s running.

Step 5: Connect to the Database

You can now connect to your PostgreSQL instance running in the container.

Using a local psql client:

psql -h localhost -p 5432 -U postgres

You will be prompted for the password you set in Step 3.

Alternatively, you can execute a psql command inside the container itself:

podman exec -it my-postgres psql -U postgres

This command runs an interactive terminal session (-it) inside the my-postgres container and executes the psql client as the postgres user.

Step 6: Stopping and Removing the Container and Volume (When Needed)

To stop the running container:

podman stop my-postgres

To remove the stopped container:

podman rm my-postgres

To remove the persistent volume (this will delete your data, so use with caution):

podman volume rm pgdata

Deploying PostgreSQL with Podman simplifies setup and management. By following these steps, you have a running, persistent PostgreSQL database instance ready for your applications. This method provides isolation and portability, making it ideal for development, testing, and production environments.

Source: https://infotechys.com/deploy-postgresql-on-podman/

900*80 ad

      1080*80 ad