This action sets up a PostgreSQL server for the rest of the job. Here are some key features:
- Runs on Linux, macOS and Windows action runners.
- Adds PostgreSQL client applications to
PATH
. - Supports PostgreSQL version parametrization.
- Easy to prove that it DOES NOT contain malicious code.
By default PostgreSQL 15 is used.
Key | Value |
---|---|
URI | postgresql://postgres:postgres@localhost/postgres |
Host | localhost |
Port | 5432 |
Username | postgres |
Password | postgres |
Database | postgres |
Service | postgres |
Key | Value |
---|---|
usesuper | true |
usecreatedb | true |
steps:
- uses: ikalnytskyi/action-setup-postgres@v5
steps:
- uses: ikalnytskyi/action-setup-postgres@v5
with:
username: ci
password: sw0rdfish
database: test
port: 34837
postgres-version: "13"
id: postgres
- run: pytest -vv tests/
env:
CONNECTION_STR: ${{ steps.postgres.outputs.connection-uri }}
- run: pytest -vv tests/
env:
CONNECTION_STR: service=${{ steps.postgres.outputs.service-name }}
steps:
- uses: ikalnytskyi/action-setup-postgres@v5
- run: |
createuser myuser
createdb --owner myuser mydatabase
psql -c "ALTER USER myuser WITH PASSWORD 'mypassword'"
env:
# This activates connection parameters for the superuser created by
# the action in the step above. It's mandatory to set this before using
# createuser/psql/etc tools.
#
# The service name is the same as the username (i.e. 'postgres') but
# it's recommended to use action's output to get the name in order to
# be forward compatible.
PGSERVICE: ${{ steps.postgres.outputs.service-name }}
shell: bash
steps:
- uses: ikalnytskyi/action-setup-postgres@v5
import psycopg
# 'postgres' is the username here, but it's recommended to use the
# action's 'service-name' output parameter here.
connection = psycopg.connect("service=postgres")
# CREATE/DROP USER statements don't work within transactions, and with
# autocommit disabled transactions are created by psycopg automatically.
connection.autocommit = True
connection.execute(f"CREATE USER myuser WITH PASSWORD 'mypassword'")
connection.execute(f"CREATE DATABASE mydatabase WITH OWNER 'myuser'")
At the time of developing there were no GitHub Actions on the marketplace to setup a PostgreSQL server on Linux, Windows and macOS action runners. Most solutions suggest using Docker which is not available on macOS and Windows runners.
The scripts and documentation in this project are released under the MIT License.