How to use docker php ext install pgsql postgres Command

Posted December 1, 2023
How to use docker php ext install pgsql postgres Command

PHP pgsql postgres extension adds functionalities to PHP for PostgreSQL database access within Dockerized applications. The pgsql and pdo_pgsql extensions allows PHP and PostgreSQL in a Docker environment to create connections.

On Docker, you must run the docker php ext install pgsql command to get the right functions of PostgreSQL within Docker containers. This guide dives deeper into the docker php ext install pgsql (pdo_pgsql) postgres command and how you use it within Docker containers.

This guide delves deeply into the docker php ext install pgsql (pdo_pgsql) postgres command with practical insights of how to run it within Docker containers.

Embark on a journey and explore the RUN docker php ext install pgsql Docker command and enhance PHP extensions to fine-tune your PHP development workflows.

Related: Redis docker php ext install and enable Redis

Step 1: Integrating RUN docker php ext install pgsql Postgres Command with Dockerfile

The docker-php-ext-install command adds PHP extensions on Docker. You’ll add a RUN docker php ext install pgsql command within a Dockerfile.

Below is an example demonstrating how Dockerfile should package docker php ext install pgsql and pdo_pgsql command:

# Use an official PHP Apache runtime
FROM php:8.2-apache
# Enable Apache modules
RUN a2enmod rewrite
# Install PostgreSQL client and its PHP extensions
RUN apt-get update \
   # pgsql headers
    && apt-get install -y libpq-dev \
    && docker-php-ext-install pgsql pdo_pgsql pdo

In this case:

  • apt-get update fetches the latest version of available packages.
  • apt-get install -y libpq-dev contains the header files and libraries needed to compile the PostgreSQL client. You must install it so PHP can connect to the PostgreSQL database.
  • docker-php-ext-install pdo pdo_pgsql Add pgsql and pdo_pgsql as PostgreSQL driver for PDO.

Step 2: Leveraging docker php ext install pgsql Command with Docker Compose

Docker Compose creates PHP and PostgreSQL containers. They will use the above Dockerfile and install the extensions to create database connections.

Define your PHP and PostgreSQL services within a docker-compose.yml file as follows:

version: '3.9'
services:
  php-app:
    # PHP Container name
    container_name: php-app
    build:
      # Dockerfile path
      context: .
      dockerfile: pgsql.dockerfile  
    volumes:
      #Coy code to the container
      - ./code:/var/www/html 
    ports:
      - 9000:80  
    depends_on:
      # postgres container start before php container
      - postgres

  # PostgreSQL Container
  pg-container:
    # Using the specified PostgreSQL image from Docker Hub
    image: postgres:latest
    environment:
      # Setting up the PostgreSQL database
      POSTGRES_DB: pgsql_db
      # Access username
      POSTGRES_USER: pgsql_user
      # username password
      POSTGRES_PASSWORD: pgsql_password
    ports:
      - "5432:5432"

Note:

  • depends_on ensures PHP’s Docker service (php-app) relies on PostgreSQL (postgres) for database connections. This way, PostgreSQL starts before PHP runs your test script.
  • environment defining PostgreSQL database configurations (password, database, users, and port) within the Docker setup are need to create PHP connection Variables.

You must have an code folder for your PHP files. This setup presupposes the following structure:

php-pgsql/
│ ├── pgsql.dockerfile
│ └── docker-compose.yml
└── code/
    └── index.php

Step 3: Establishing PHP PostgreSQL Connection on Docker

To test if the pgsql extension is installed correctly and working within a Dockerized PHP environment, code/index.php should have scripts to interact with a PostgreSQL database as follows:

<?php
$postgres_host = 'pg-container'; // This corresponds to the service name in docker-compose.yml
$user = 'pgsql_user'; 
$password = 'pgsql_password';
$dbname = 'pgsql_db';
$pgsql_port = '5432'; // PostgreSQL default port

$conn = pg_connect("host=$host port=$pgsql_port dbname=$dbname user=$user password=$password");
if (!$conn) {
    echo "Connection failed ";
} else {
    echo "PostgreSQL Connected successfully!";
}
?>

OR use PDO as follows:

<?php
$postgres_host = 'pg-container';
$user = 'pgsql_user';
$pass = 'pgsql_password';
$database = 'pgsql_db';

$conn = new PDO("pgsql:host=$postgres_host;dbname=$database", $user, $pass);

if (!$conn) {
    die("Connection failed");
}

echo "Connected to PostgreSQL successfully";
$conn = null;
?>

Key considerations:

  • The host for the PostgreSQL database isn’t set as ’localhost.’ When working within Docker, the host changes to the name of the PostgreSQL container, which here is referred to as pg-container.
  • Your Docker setup includes the pgsql extension installed using the command docker-php-ext-install pgsql in the Dockerfile. Variables in the PostgreSQL Docker environment, like POSTGRES_DB: pgsql_db and POSTGRES_USER: pgsql_user, should align with the variables used in your PHP script, i.e., $user,postgres_host, $pass, and $database.

Step 4: Testing the Functionality of pgsql with PostgreSQL on Docker

Initiate your PHP container using the docker-compose command:

docker-compose up --build -d

Building images:

How to use docker php ext install pgsql postgres Command

Check your running containers using the command:

How to use docker php ext install pgsql postgres Command

Navigate to http://localhost:9000/ in your web browser. You should have a successful connection between pgsql and your database:

How to use docker php ext install pgsql postgres Command

Your Dockerized PHP environment is running and connected to the PostgreSQL database through pgsql.

Step 5: Accessing PHP docker-php-ext-install pgsql Database on Docker

To access your database, use (GUI) like pgAdmin4. ThisRunning PHP, Apache, and PostgreSQL with Docker Compose provides an extensive walkthrough to PHP, Docker, PostgreSQL, and pgAdmin.

Conclusion

You have successfully used the docker-php-ext-install pgsql command as your PostgreSQL extension for PHP within Docker. You can now confidently use the RUN docker-php-ext-install pgsql command to integrate PHP and PostgreSQL in a Docker environment.

How to use docker php ext install pgsql postgres Command

Written By:

Joseph Chege