Quick starter PHP and PostgreSQL Project
The Project
Recently, I had to take over a project that was required to be written on PHP and use PostgreSQL. I had no previous knowledge on both prior to this, so a little googling was required. Here is the quick setup I've been using for developing.
Docker
As I love Docker, I decided to use the PHP(Apache) image, but, as I found out later, it was necessary to install some stuff (1) (2) to let PHP to connect with PostgreSQL. Thankfully, the internet is full of helpful people; so the necessary Dockerfile to do this is:
FROM php:7.0-apache
RUN apt-get update && apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql
Then, this Docker-Compose file is going to get us our custom PHP-Apache image, PostgreSQL and pgAdmin to let us setup our database.
version: "3.9"
services:
  postgresql:
    image: postgres:11.0
    ports:
      - 5432:5432
    restart: unless-stopped
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: php_database
    volumes:
      - postgresql-volume:/var/lib/postgresql/data
    networks:
      - php-pgsql-network
  pgadmin:
    image: dpage/pgadmin4:6.8
    ports:
      - 5050:80
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: root@example.org
      PGADMIN_DEFAULT_PASSWORD: root
    volumes:
      - pgadmin-volume:/var/lib/pgadmin
    networks:
      - php-pgsql-network
  apache-php:
    build:
      context: ./
      dockerfile: php.Dockerfile
    ports:
      - 7070:80
    restart: unless-stopped
    volumes:
      - /path/to/the/php/code:/var/www/html
    networks:
      - php-pgsql-network
networks:
  php-pgsql-network:
volumes:
  postgresql-volume:
  pgadmin-volume:
Start it with:
docker-compose -f php-compose.yml up -d
pgAdmin caveats and DBeaver
I had some problems to connect to a production database using pgAdmin, so the workaround was to use DBeaver. Now we have two similar tools for the DB administration.
Next Steps
Well, for now, we just have to put the PHP code in the /path/to/the/php/code directory and play with the PostgreSQL database using pgAdmin or DBeaver. For now, this is enough, but in the next posts I'll be sharing some PHP code.