1
1
# setup-postgres
2
2
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 )
6
6
7
7
This action sets up a PostgreSQL server for the rest of the job. Here are some
8
8
key features:
9
9
10
10
* Runs on Linux, macOS and Windows action runners.
11
- * Adds PostgreSQL [ client applications] [ 1 ] to ` PATH ` .
12
11
* PostgreSQL version can be parametrized.
13
- * Supports SSL if needed.
12
+ * Adds PostgreSQL [ client applications] [ 1 ] to ` PATH ` .
13
+ * Supports SSL on-demand.
14
14
* Easy [ to verify] [ 2 ] that it DOES NOT contain malicious code.
15
15
16
- By default PostgreSQL 15 is used.
17
-
18
16
[ 1 ] : https://www.postgresql.org/docs/current/reference-client.html
19
17
[ 2 ] : action.yml
20
18
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
37
20
38
21
| Key | Value | Default |
39
22
| ------------------| ------------------------------------------------------------------------------------| -------------|
@@ -58,14 +41,55 @@ By default PostgreSQL 15 is used.
58
41
| usesuper | true |
59
42
| usecreatedb | true |
60
43
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
+
61
65
#### Basic
62
66
63
67
``` yaml
64
68
steps :
65
69
- 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 }}
66
90
` ` `
67
91
68
- #### Advanced
92
+ #### Parametrized
69
93
70
94
` ` ` yaml
71
95
steps :
@@ -76,61 +100,31 @@ steps:
76
100
database : test
77
101
port : 34837
78
102
postgres-version : " 14"
79
- ssl : " on "
103
+ ssl : true
80
104
id : postgres
81
105
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"
87
109
env :
88
- CONNECTION_STR : service= ${{ steps.postgres.outputs.service-name }}
110
+ PGSERVICE : ${{ steps.postgres.outputs.service-name }}
89
111
` ` `
90
112
91
- ## Recipes
113
+ ## FAQ
92
114
93
- #### Create a new user w/ database via CLI
115
+ #### How do I create a new database with a new user?
94
116
95
117
` ` ` yaml
96
118
steps :
97
119
- uses : ikalnytskyi/action-setup-postgres@v6
120
+ id : postgres
98
121
99
- - run : |
122
+ - env :
123
+ PGSERVICE : " ${{ steps.postgres.outputs.service-name }}"
124
+ run : |
100
125
createuser myuser
101
126
createdb --owner myuser mydatabase
102
127
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'")
134
128
` ` `
135
129
136
130
## Rationale
0 commit comments