Skip to content
database

GitHub Action

Setup PostgreSQL for Linux/macOS/Windows

v7 Latest version

Setup PostgreSQL for Linux/macOS/Windows

database

Setup PostgreSQL for Linux/macOS/Windows

Setup a preinstalled PostgreSQL server

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Setup PostgreSQL for Linux/macOS/Windows

uses: ikalnytskyi/action-setup-postgres@v7

Learn more about this action in ikalnytskyi/action-setup-postgres

Choose a version

setup-postgres

CI build GitHub Marketplace

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.
  • PostgreSQL version can be parametrized.
  • Adds PostgreSQL client applications to PATH.
  • Supports SSL on-demand.
  • Easy to verify that it DOES NOT contain malicious code.

Inputs

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
postgres-version The PostgreSQL major version to install. Supported values: "14", "15", "16", "17". 17

Outputs

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
certificate-path The path to the server certificate if SSL is on. /home/runner/work/_temp/pgdata/server.crt

User permissions

Key Value
usesuper true
usecreatedb true

Usage

Important

In order to connect to a PostgreSQL server, either use connection parameters directly (see basic example), or, preferably, obtain a connection URI from the connection-uri output (see recommended example).

Tip

For libpq-based applications, such as PostgreSQL client applications, set the PGSERVICE=postgres environment variable to automatically use the correct connection parameters (see example). The postgres value corresponds to the service name from the service-name output.

Basic

steps:
  - uses: ikalnytskyi/action-setup-postgres@v7

  - run: psql postgresql://postgres:postgres@localhost:5432/postgres -c "SELECT 1"
  - run: psql service=postgres -c "SELECT 1"
  - run: psql -c "SELECT 1"
    env:
      PGSERVICE: postgres

Recommended

steps:
  - uses: ikalnytskyi/action-setup-postgres@v7
    id: postgres

  - run: psql ${{ steps.postgres.outputs.connection-uri }} -c "SELECT 1"
  - run: psql service=${{ steps.postgres.outputs.service-name }} -c "SELECT 1"
  - run: psql -c "SELECT 1"
    env:
      PGSERVICE: ${{ steps.postgres.outputs.service-name }}

Parametrized

steps:
  - uses: ikalnytskyi/action-setup-postgres@v7
    with:
      username: ci
      password: sw0rdfish
      database: test
      port: 34837
      postgres-version: "14"
      ssl: true
    id: postgres

  - run: psql ${{ steps.postgres.outputs.connection-uri }} -c "SELECT 1"
  - run: psql service=${{ steps.postgres.outputs.service-name }} -c "SELECT 1"
  - run: psql -c "SELECT 1"
    env:
      PGSERVICE: ${{ steps.postgres.outputs.service-name }}

FAQ

How do I create a new database with a new user?

steps:
  - uses: ikalnytskyi/action-setup-postgres@v7
    id: postgres

  - env:
      PGSERVICE: "${{ steps.postgres.outputs.service-name }}"
    run: |
      createuser myuser
      createdb --owner myuser mydatabase
      psql -c "ALTER USER myuser WITH PASSWORD 'mypassword'"

Rationale

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.

License

The scripts and documentation in this project are released under the MIT License.