11# setup-postgres
22
3- [ ![ GitHub ] ( https://img.shields.io/badge/ github- ikalnytskyi/action-- setup-- postgres-8da0cb? logo=github )] ( https://github.com/ikalnytskyi/action-setup-postgres )
4- [ ![ CI build ] ( https://github.com/ ikalnytskyi/action-setup-postgres/actions/workflows/ci.yml/ badge.svg )] ( https://github.com/ikalnytskyi/action-setup-postgres/actions )
5- [ ![ Marketplace] ( https://img.shields.io/badge/market-setup--postgres-6F42C1 ?logo=github )] ( https://github.com/marketplace/actions/setup-postgresql-for-linux-macos-windows )
3+ [ ![ CI build ] ( https://img.shields.io/github/actions/workflow/status/ ikalnytskyi/action-setup-postgres/ci.yml?style=for-the-badge& logo=github&label=Tests )] ( https://github.com/ikalnytskyi/action-setup-postgres/actions )
4+ [ ![ GitHub ] ( https://img.shields.io/badge/github- ikalnytskyi/action-- setup-- postgres-3795BD?logo=github&style=for-the- badge )] ( https://github.com/ikalnytskyi/action-setup-postgres )
5+ [ ![ Marketplace] ( https://img.shields.io/badge/market-setup--postgres-4E31AA ?logo=github&style=for-the-badge )] ( https://github.com/marketplace/actions/setup-postgresql-for-linux-macos-windows )
66
77This action sets up a PostgreSQL server for the rest of the job. Here are some
88key features:
99
1010* Runs on Linux, macOS and Windows action runners.
11- * Adds PostgreSQL [ client applications] [ 1 ] to ` PATH ` .
1211* PostgreSQL version can be parametrized.
13- * Supports SSL if needed.
12+ * Adds PostgreSQL [ client applications] [ 1 ] to ` PATH ` .
13+ * Supports SSL on-demand.
1414* Easy [ to verify] [ 2 ] that it DOES NOT contain malicious code.
1515
16- By default PostgreSQL 15 is used.
17-
1816[ 1 ] : https://www.postgresql.org/docs/current/reference-client.html
1917[ 2 ] : action.yml
2018
21- ## Usage
22-
23- > [ !IMPORTANT]
24- >
25- > In order to connect to a PostgreSQL server, use either connection parameters
26- > from the table below ([ link] ( #outputs ) ), or retrieve a
27- > connection URI from the ` connection-uri ` output ([ link] ( #advanced ) ).
28-
29- > [ !TIP]
30- >
31- > ` libpq ` -using applications may choose to set the ` PGSERVICE=postgres `
32- > environment variable instead ([ link] ( #create-a-new-user-w-database-via-cli ) ),
33- > where ` postgres ` is the service name extracted from the ` service-name `
34- > output.
35-
36- #### Action Parameters
19+ #### Inputs
3720
3821| Key | Value | Default |
3922| ------------------| ------------------------------------------------------------------------------------| -------------|
@@ -58,14 +41,55 @@ By default PostgreSQL 15 is used.
5841| usesuper | true |
5942| usecreatedb | true |
6043
44+
45+ ## Usage
46+
47+ > [ !IMPORTANT]
48+ >
49+ > In order to connect to a PostgreSQL server, either use connection parameters
50+ > directly (see [ basic] example), or, preferably, obtain a connection URI from
51+ > the ` connection-uri ` output (see [ recommended] example).
52+ >
53+ > [ basic ] : #basic
54+ > [ recommended ] : #recommended
55+
56+ > [ !TIP]
57+ >
58+ > For ` libpq ` -based applications, such as PostgreSQL client applications, set
59+ > the ` PGSERVICE=postgres ` environment variable to automatically use the
60+ > correct connection parameters (see [ example] ). The ` postgres ` value
61+ > corresponds to the service name from the ` service-name ` output.
62+ >
63+ > [ example ] : #how-do-i-create-a-new-database-with-a-new-user
64+
6165#### Basic
6266
6367``` yaml
6468steps :
6569 - uses : ikalnytskyi/action-setup-postgres@v6
70+
71+ - run : psql postgresql://postgres:postgres@localhost:5432/postgres -c "SELECT 1"
72+ - run : psql service=postgres -c "SELECT 1"
73+ - run : psql -c "SELECT 1"
74+ env :
75+ PGSERVICE : postgres
76+ ` ` `
77+
78+ #### Recommended
79+
80+ ` ` ` yaml
81+ steps :
82+ - uses : ikalnytskyi/action-setup-postgres@v6
83+ id : postgres
84+
85+ - run : psql ${{ steps.postgres.outputs.connection-uri }} -c "SELECT 1"
86+ - run : psql service=${{ steps.postgres.outputs.service-name }} -c "SELECT 1"
87+ - run : psql -c "SELECT 1"
88+ env :
89+ PGSERVICE : ${{ steps.postgres.outputs.service-name }}
6690` ` `
6791
68- #### Advanced
92+ #### Parametrized
6993
7094` ` ` yaml
7195steps :
@@ -76,61 +100,31 @@ steps:
76100 database : test
77101 port : 34837
78102 postgres-version : " 14"
79- ssl : " on "
103+ ssl : true
80104 id : postgres
81105
82- - run : pytest -vv tests/
83- env :
84- CONNECTION_STR : ${{ steps.postgres.outputs.connection-uri }}
85-
86- - run : pytest -vv tests/
106+ - run : psql ${{ steps.postgres.outputs.connection-uri }} -c "SELECT 1"
107+ - run : psql service=${{ steps.postgres.outputs.service-name }} -c "SELECT 1"
108+ - run : psql -c "SELECT 1"
87109 env :
88- CONNECTION_STR : service= ${{ steps.postgres.outputs.service-name }}
110+ PGSERVICE : ${{ steps.postgres.outputs.service-name }}
89111` ` `
90112
91- ## Recipes
113+ ## FAQ
92114
93- #### Create a new user w/ database via CLI
115+ #### How do I create a new database with a new user?
94116
95117` ` ` yaml
96118steps :
97119 - uses : ikalnytskyi/action-setup-postgres@v6
120+ id : postgres
98121
99- - run : |
122+ - env :
123+ PGSERVICE : " ${{ steps.postgres.outputs.service-name }}"
124+ run : |
100125 createuser myuser
101126 createdb --owner myuser mydatabase
102127 psql -c "ALTER USER myuser WITH PASSWORD 'mypassword'"
103- env:
104- # This activates connection parameters for the superuser created by
105- # the action in the step above. It's mandatory to set this before using
106- # createuser/psql and other libpq-using applications.
107- #
108- # The service name is the same as the username (i.e. 'postgres') but
109- # it's recommended to use action's output to get the name in order to
110- # be forward compatible.
111- PGSERVICE: ${{ steps.postgres.outputs.service-name }}
112- shell: bash
113- ` ` `
114-
115- #### Create a new user w/ database via psycopg
116-
117- ` ` ` yaml
118- steps :
119- - uses : ikalnytskyi/action-setup-postgres@v6
120- ` ` `
121-
122- ` ` ` python
123- import psycopg
124-
125- # 'postgres' is the username here, but it's recommended to use the
126- # action's 'service-name' output parameter here.
127- connection = psycopg.connect("service=postgres")
128-
129- # CREATE/DROP USER statements don't work within transactions, and with
130- # autocommit disabled transactions are created by psycopg automatically.
131- connection.autocommit = True
132- connection.execute(f"CREATE USER myuser WITH PASSWORD 'mypassword'")
133- connection.execute(f"CREATE DATABASE mydatabase WITH OWNER 'myuser'")
134128` ` `
135129
136130## Rationale
0 commit comments