@@ -21,6 +21,10 @@ inputs:
2121 description : The server port to listen on.
2222 default : " 5432"
2323 required : false
24+ postgres-version :
25+ description : The PostgreSQL major version to install. Either "14", "15", or "16".
26+ default : " 16"
27+ required : false
2428outputs :
2529 connection-uri :
2630 description : The connection URI to connect to PostgreSQL.
@@ -31,38 +35,63 @@ outputs:
3135runs :
3236 using : composite
3337 steps :
34- - name : Prerequisites
38+ - name : Install PostgreSQL
3539 run : |
40+ if [[ ! "${{ inputs.postgres-version }}" =~ ^(14|15|16)$ ]]; then
41+ echo "::error::postgres-version must be one of: 14, 15, 16."
42+ exit 1
43+ fi
44+
3645 if [ "$RUNNER_OS" == "Linux" ]; then
37- echo "$(pg_config --bindir)" >> $GITHUB_PATH
38- elif [ "$RUNNER_OS" == "Windows" ]; then
39- echo "$PGBIN" >> $GITHUB_PATH
40- echo "PQ_LIB_DIR=$PGROOT\lib" >> $GITHUB_ENV
46+ APT_ENTRY="deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main"
47+ APT_KEY="https://www.postgresql.org/media/keys/ACCC4CF8.asc"
48+
49+ echo "$APT_ENTRY" | sudo tee /etc/apt/sources.list.d/pgdg.list
50+ wget --quiet -O - "$APT_KEY" | sudo apt-key add -
51+ sudo apt-get update
52+ sudo apt-get -y install postgresql-${{ inputs.postgres-version }}
53+
54+ PG_BINDIR=$("/usr/lib/postgresql/${{ inputs.postgres-version }}/bin/pg_config" --bindir)
55+ echo "$PG_BINDIR" >> $GITHUB_PATH
4156
57+ elif [ "$RUNNER_OS" == "Windows" ]; then
4258 # The Windows runner has some PostgreSQL environment variables set
4359 # that may confuse users since they may be irrelevant to the
44- # PostgreSQL server we're using.
60+ # PostgreSQL server we're using. Since GitHub actions does not
61+ # support unsetting environment variables, the best we can do is to
62+ # clear their values in order to indicate they must not be used.
4563 for name in "PGROOT" "PGDATA" "PGBIN" "PGUSER" "PGPASSWORD"; do
4664 echo "$name=" >> $GITHUB_ENV
4765 done
66+
67+ choco install postgresql${{ inputs.postgres-version }} \
68+ --params "/Password:${{ inputs.password }}" \
69+ --ia "--enable-components server,commandlinetools --extract-only 1" \
70+ --no-progress
71+
72+ PG_BINDIR=$("$PROGRAMFILES/PostgreSQL/${{ inputs.postgres-version }}/bin/pg_config.exe" --bindir)
73+ PG_LIBDIR=$("$PROGRAMFILES/PostgreSQL/${{ inputs.postgres-version }}/bin/pg_config.exe" --libdir)
74+
75+ echo "$PG_BINDIR" >> $GITHUB_PATH
76+ echo "PQ_LIB_DIR=$PG_LIBDIR" >> $GITHUB_ENV
77+
4878 elif [ "$RUNNER_OS" == "macOS" ]; then
49- case "$(sw_vers -productVersion)" in
50- 13.*|14.*)
51- # Unfortunately, the macOS 13 runner image doesn't come w/
52- # pre-installed PostgreSQL server.
53- export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
54- export HOMEBREW_NO_INSTALL_CLEANUP=1
55- export HOMEBREW_NO_INSTALL_UPGRADE=1
56- brew install --skip-post-install postgresql@14
57- ;;
58- esac
79+ export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
80+ export HOMEBREW_NO_INSTALL_CLEANUP=1
81+ export HOMEBREW_NO_INSTALL_UPGRADE=1
82+ brew install --skip-post-install postgresql@${{ inputs.postgres-version }}
83+
84+ # Link PostgreSQL binaries from /usr/local/bin in order to make them
85+ # available globally. The overwrite option is required since some
86+ # GitHub runners come with preinstalled PostgreSQL binaries.
87+ brew link --overwrite postgresql@${{ inputs.postgres-version }}
5988 fi
6089 shell : bash
6190
6291 - name : Setup and start PostgreSQL
6392 run : |
64- export PGDATA="$RUNNER_TEMP/pgdata"
65- export PWFILE="$RUNNER_TEMP/pwfile"
93+ PGDATA="$RUNNER_TEMP/pgdata"
94+ PWFILE="$RUNNER_TEMP/pwfile"
6695
6796 DEFAULT_ENCODING="UTF-8"
6897 DEFAULT_LOCALE="en_US.$DEFAULT_ENCODING"
91120 #
92121 # [1] https://www.postgresql.org/docs/15/reference-client.html
93122 initdb \
123+ --pgdata="$PGDATA" \
94124 --username="${{ inputs.username }}" \
95125 --pwfile="$PWFILE" \
96126 --auth="scram-sha-256" \
@@ -102,7 +132,7 @@ runs:
102132 # directory we have no permissions to (owned by system postgres user).
103133 echo "unix_socket_directories = ''" >> "$PGDATA/postgresql.conf"
104134 echo "port = ${{ inputs.port }}" >> "$PGDATA/postgresql.conf"
105- pg_ctl start
135+ pg_ctl start --pgdata="$PGDATA"
106136
107137 # Save required connection parameters for created superuser to the
108138 # connection service file [1]. This allows using these connection
0 commit comments