GitHub Action
Setup PostgreSQL and PostGIS for Linux/macOS/Windows
This GitHub action sets up a PostgreSQL server with PostGIS extension. The code is based on the ikalnytskyi/action-setup-postgres action.
- Runs on Linux, macOS and Windows GitHub runners.
- Adds PostgreSQL binaries (e.g.
psql
) toPATH
. - Uses PostgreSQL installed in GitHub Actions Virtual Environments.
- Installs the correct version of PostGIS and runs
CREATE EXTENSION postgis
in the new database.- Linux version is installed from the PostGIS apt repository.
- Windows version is installed from the OSGeo.
- MacOS version is installed using Homebrew package.
- Easy to check that IT DOES NOT contain malicious code.
See also action-setup-nginx to configure NGINX service.
steps:
- uses: nyurik/action-setup-postgis@v2
id: postgres
- run: psql "$DB_CONN_STR" -c 'SELECT 1;'
env:
DB_CONN_STR: ${{ steps.postgres.outputs.connection-uri }}
Important
In order to connect to a PostgreSQL server, use either connection parameters
from the table below (link), or retrieve a
connection URI from the connection-uri
output (link).
Tip
libpq
-using applications may choose to set the PGSERVICE=postgres
environment variable instead (link),
where postgres
is the service name extracted from the service-name
output.
Key | Value | Default |
---|---|---|
username | The username of the user to setup. | postgres |
password | The password of the user to setup. | postgres |
database | The database name to setup and grant permissions to created user. | postgres |
port | The server port to listen on. | 5432 |
cached-dir | Where should the temporary downloads be placed. Used to download and cache PostGIS binary. | downloads |
Key | Description | Example |
---|---|---|
connection-uri | The connection URI to connect to PostgreSQL. | postgresql://postgres:postgres@localhost/postgres |
service-name | The service name with connection parameters. | postgres |
Key | Value |
---|---|
usesuper | true |
usecreatedb | true |
steps:
- uses: nyurik/action-setup-postgis@v2
with:
username: ci
password: sw0rdfish
database: test
port: 34837
id: postgres
- run: psql "$CONNECTION_STR" -c 'SELECT 1;'
env:
CONNECTION_STR: ${{ steps.postgres.outputs.connection-uri }}
steps:
- uses: nyurik/action-setup-postgis@v2
id: postgres
- 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 and other libpq-using applications.
#
# 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: nyurik/action-setup-postgis@v2
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.