postgresqlgithubgithub-actionscicd

How to set up PostgreSQL in GitHub Actions workflow


I am trying to use GitHub Actions to test my project automatically, but am having trouble getting it to build. My project uses PostgreSQL as a dependency, it installs but doesn't respond over the network.

This is my django.yml workflow:

name: Django CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.7, 3.8, 3.9]

    steps:
    - name: Install Dependencies
      run: |
        sudo apt update
        sudo apt install -y postgresql postgresql-contrib
        pip install django
        sudo apt install -y python3-dev libpq-dev
        pip install psycopg2
        sudo -u postgres psql -f setup.sql
        python manage.py migrate
    - name: Run Tests
      run: |
        python manage.py test core

Here are the console logs:

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Hit:1 http://azure.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://azure.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://azure.archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
Get:4 http://azure.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:5 https://packages.microsoft.com/ubuntu/22.04/prod jammy InRelease [3065 B]
Get:6 http://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [943 kB]
Get:7 http://azure.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [204 kB]
Get:8 http://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [13.6 kB]
Get:9 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [883 kB]
Get:10 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [174 kB]
Get:11 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [18.0 kB]
Hit:12 https://ppa.launchpadcontent.net/ubuntu-toolchain-r/test/ubuntu jammy InRelease
Get:13 http://azure.archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [687 kB]
Get:14 http://azure.archive.ubuntu.com/ubuntu jammy-security/main Translation-en [141 kB]
Get:15 http://azure.archive.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [8832 B]
Get:16 http://azure.archive.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [702 kB]
Get:17 http://azure.archive.ubuntu.com/ubuntu jammy-security/universe Translation-en [112 kB]
Get:18 http://azure.archive.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [13.6 kB]
Get:19 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main armhf Packages [6252 B]
Get:20 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 Packages [52.8 kB]
Get:21 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main all Packages [716 B]
Get:22 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main arm64 Packages [11.6 kB]
Fetched 4311 kB in 1s (4079 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
26 packages can be upgraded. Run 'apt list --upgradable' to see them.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  postgresql-doc
The following NEW packages will be installed:
  postgresql postgresql-contrib
0 upgraded, 2 newly installed, 0 to remove and 26 not upgraded.
Need to get 6580 B of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://azure.archive.ubuntu.com/ubuntu jammy/main amd64 postgresql all 14+238 [3288 B]
Get:2 http://azure.archive.ubuntu.com/ubuntu jammy/main amd64 postgresql-contrib all 14+238 [3292 B]
Fetched 6580 B in 0s (16.0 kB/s)
Selecting previously unselected package postgresql.
(Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 263190 files and directories currently installed.)
Preparing to unpack .../postgresql_14+238_all.deb ...
Unpacking postgresql (14+238) ...
Selecting previously unselected package postgresql-contrib.
Preparing to unpack .../postgresql-contrib_14+238_all.deb ...
Unpacking postgresql-contrib (14+238) ...
Setting up postgresql-contrib (14+238) ...
Setting up postgresql (14+238) ...
NEEDRESTART-VER: 3.5
NEEDRESTART-KCUR: 5.15.0-1034-azure
NEEDRESTART-KEXP: 5.15.0-1034-azure
NEEDRESTART-KSTA: 1
Defaulting to user installation because normal site-packages is not writeable
Collecting django
  Downloading Django-4.1.7-py3-none-any.whl (8.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB 18.9 MB/s eta 0:00:00
Collecting asgiref<4,>=3.5.2
  Downloading asgiref-3.6.0-py3-none-any.whl (23 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.3-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.8/42.8 KB 12.3 MB/s eta 0:00:00
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.6.0 django-4.1.7 sqlparse-0.4.3

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
python3-dev is already the newest version (3.10.6-1~22.04).
libpq-dev is already the newest version (15.2-1.pgdg22.04+1).
0 upgraded, 0 newly installed, 0 to remove and 26 not upgraded.
Defaulting to user installation because normal site-packages is not writeable
Collecting psycopg2
  Downloading psycopg2-2.9.5.tar.gz (384 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.3/384.3 KB 6.6 MB/s eta 0:00:00
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py): started
  Building wheel for psycopg2 (setup.py): finished with status 'done'
  Created wheel for psycopg2: filename=psycopg2-2.9.5-cp310-cp310-linux_x86_64.whl size=499016 sha256=ec7d50cba0fce65cdd5e5ac9183bf7877f4feb3f3f0dc6de08800d2f3ece4c40
  Stored in directory: /home/runner/.cache/pip/wheels/b4/83/91/37687788ee41584610b6db5b85f98ed987fffb2ca193558264
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.5
could not change directory to "/home/runner/work/pelican/pelican": Permission denied
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
Error: Process completed with exit code 2.

I tried to set up a VM with my vagrantfile, but I couldn't get that to work and all my internet searching came up with no good results.


Solution

  • PostgreSQL is preinstalled on that runner but it is disabled by default. You just have to start its service:

    User: postgres
    PostgreSQL service is disabled by default.
    Use the following command as a part of your job to start the service: 'sudo systemctl start postgresql.service'
    

    Run:

    sudo systemctl start postgresql.service
    

    before the rest of the workflow that depends on it.

    Also, to keep your Python/Django app self-contained, try to use venv so that you won't run into issues with globally installed dependencies.


    Regarding Vagrant, you can use it with macos-12 GHA-hosted runner where VirtualBox and vagrant are installed.

    However, the preinstalled PostgreSQL should be enough for your purposes. Even if you need to install a specific version, you can use service containers. See Creating PostgreSQL service containers for more details.