Deploy PostgreSQL Container with Docker Compose

Introduction

PostgreSQL, often referred to as “Postgres,” is a powerful open-source relational database management system (RDBMS). It is known for its robustness, extensibility, and compliance with SQL standards. PostgreSQL is designed to handle various types of workloads, from small single-machine applications to large enterprise and web-based systems.

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in linux commands.
  • Internet connectivity.
  • Docker and docker compose should installed on your machine.

To install Docker and docker compose on ubuntu 22.04 LTS, We can use the given link.

In this post, We will deploy PostgreSQL database container using Docker Compose on Ubuntu 22.04 LTS machine.

Step 1: Creating docker-compose.yml

Using Docker Compose simplifies the process of deploying and managing multi-container Docker applications. Here are the steps to deploy a PostgreSQL container with Docker Compose:

To create a docker-compose.yml

sudo nano docker-compose.yml

Copy and paste the followings configuration.

version: '3'

services:
  postgres:
    image: postgres
    container_name: your-postgres-container
    environment:
      POSTGRES_PASSWORD: your-postgres-password
    ports:
      - "5432:5432"

Explanation of the fields in this docker-compose.yml file:

  • image: Specifies the Docker image to use (in this case, the official PostgreSQL image).
  • container_name: Sets the name for your PostgreSQL container.
  • ports: Maps the container’s PostgreSQL port (5432) to the host machine’s port (5432).

Save and exit form nano text editor.

Step 2: Deploy PostgreSQL Container

In the directory where your docker-compose.yml file is located, run the following command to start the PostgreSQL container:

sudo docker-compose up -d

The -d flag runs the containers in the background, It will take few sec or mins to pull the image and deploy the PostgresSQL.

Step 3: Validate the Container

To check the running containers launched by Docker Compose, you can use the following command:

sudo docker-compose ps

This command provides a summary of the status of each service defined in your docker-compose.yml file. It shows information such as the service name, container ID, status, ports, and names.

Step 4: Logging

Some time we need to also check the real time logs, So we can use the given commands.

sudo docker-compose logs -f postgres

Step 5: PostgreSQL connection PHP

To connect to a PostgreSQL database using PHP, you can use the pg_connect function or, preferably, the PDO (PHP Data Objects) extension for more flexibility and security. Below are examples of both methods:

Using pg_connect:

<?php
// Replace these variables with your actual database credentials
$host = "your_host";
$port = "your_port";
$dbname = "your_database";
$user = "your_user";
$password = "your_password";

// Connection string
$conn_string = "host={$host} port={$port} dbname={$dbname} user={$user} password={$password}";

// Connect to PostgreSQL
$dbconn = pg_connect($conn_string);

// Check connection
if (!$dbconn) {
    die("Connection failed: " . pg_last_error());
}

echo "Connected successfully to PostgreSQL.";

// Close the connection
pg_close($dbconn);
?>

Using PDO:

<?php
// Replace these variables with your actual database credentials
$host = "your_host";
$port = "your_port";
$dbname = "your_database";
$user = "your_user";
$password = "your_password";

// Connection string
$conn_string = "pgsql:host={$host};port={$port};dbname={$dbname};user={$user};password={$password}";

try {
    // Create a PDO instance
    $pdo = new PDO($conn_string);

    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "Connected successfully to PostgreSQL using PDO.";
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
} finally {
    // Close the connection
    $pdo = null;
}
?>

Replace the placeholder values (your_host, your_port, your_database, your_user, your_password) with your actual PostgreSQL database connection details.

Note: Using PDO is generally recommended because it provides a consistent interface for working with different database systems and helps prevent SQL injection through prepared statements.

Step 6: Destroy PostgreSQL Container

To destroy (stop and remove) the PostgreSQL container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml file is located:

sudo docker-compose down

This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml file.

The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v option:

sudo docker-compose down -v

Make sure you are in the correct directory containing your docker-compose.yml file when running these commands. This ensures that Docker Compose identifies the correct configuration file.

After running the docker-compose down command, you can use the docker-compose ps command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.

Remember that this command will stop and remove all the services defined in your docker-compose.yml file, not just the PostgreSQL service. If you only want to remove a specific service, you can specify the service name:

docker-compose down -v <service_name>

Conclusion

We have successfully deployed PostgreSQL database server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.

Author

Deploy PostgreSQL Container with Docker Compose

Leave a Reply

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

Scroll to top