summaryrefslogtreecommitdiff
path: root/user/postgresql
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2018-06-14 02:22:19 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2018-06-14 02:22:56 -0500
commitb1eb50599e4db7eb4501af75cbbfa22007081ea5 (patch)
treec94a34c882cc17adedd781e8c5f34349b2a62416 /user/postgresql
parentb9e85bbdcf38547ef2ca4e5c2a6e6293bbcd2752 (diff)
downloadpackages-b1eb50599e4db7eb4501af75cbbfa22007081ea5.tar.gz
packages-b1eb50599e4db7eb4501af75cbbfa22007081ea5.tar.bz2
packages-b1eb50599e4db7eb4501af75cbbfa22007081ea5.tar.xz
packages-b1eb50599e4db7eb4501af75cbbfa22007081ea5.zip
The New Plan
all pkgs needed to bootstrap -> system others -> user
Diffstat (limited to 'user/postgresql')
-rw-r--r--user/postgresql/APKBUILD259
-rw-r--r--user/postgresql/conf-unix_socket_directories.patch14
-rw-r--r--user/postgresql/disable-broken-tests.patch119
-rw-r--r--user/postgresql/initdb.patch14
-rw-r--r--user/postgresql/perl-rpath.patch22
-rw-r--r--user/postgresql/pg-restore.confd16
-rw-r--r--user/postgresql/pg-restore.initd36
-rw-r--r--user/postgresql/pltcl_create_tables.sql13
-rw-r--r--user/postgresql/postgresql.confd59
-rw-r--r--user/postgresql/postgresql.initd220
-rw-r--r--user/postgresql/postgresql.pre-upgrade32
11 files changed, 804 insertions, 0 deletions
diff --git a/user/postgresql/APKBUILD b/user/postgresql/APKBUILD
new file mode 100644
index 000000000..e618eeb39
--- /dev/null
+++ b/user/postgresql/APKBUILD
@@ -0,0 +1,259 @@
+# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
+# Contributor: G.J.R. Timmer <gjr.timmer@gmail.com>
+# Contributor: Jakub Jirutka <jakub@jirutka.cz>
+pkgname=postgresql
+pkgver=10.0
+pkgrel=0
+pkgdesc="A sophisticated object-relational DBMS"
+url="http://www.postgresql.org/"
+arch="all"
+license="BSD"
+depends="postgresql-client tzdata"
+options="!checkroot"
+install="$pkgname.pre-upgrade"
+pkgusers="postgres"
+pkggroups="postgres"
+checkdepends="diffutils"
+depends_dev="openssl-dev"
+makedepends="$depends_dev libedit-dev zlib-dev libxml2-dev util-linux-dev
+ openldap-dev tcl-dev perl-dev python3-dev"
+subpackages="$pkgname-contrib $pkgname-dev $pkgname-doc libpq $pkgname-libs
+ $pkgname-client $pkgname-pltcl
+ $pkgname-plperl $pkgname-plperl-contrib:plperl_contrib
+ $pkgname-plpython3 $pkgname-plpython3-contrib:plpython3_contrib"
+source="https://ftp.postgresql.org/pub/source/v$pkgver/$pkgname-$pkgver.tar.bz2
+ initdb.patch
+ perl-rpath.patch
+ conf-unix_socket_directories.patch
+ disable-broken-tests.patch
+ $pkgname.initd
+ $pkgname.confd
+ pg-restore.initd
+ pg-restore.confd
+ pltcl_create_tables.sql
+ "
+builddir="$srcdir/$pkgname-$pkgver"
+options="!checkroot"
+
+# secfixes:
+# 9.6.4-r0:
+# - CVE-2017-7546
+# - CVE-2017-7547
+# - CVE-2017-7548
+# 9.6.3-r0:
+# - CVE-2017-7484
+# - CVE-2017-7485
+# - CVE-2017-7486
+
+prepare() {
+ default_prepare
+ cd "$builddir"
+
+ local file; for file in $pkgname.initd $pkgname.confd; do
+ sed "s|@VERSION@|${pkgver%.*}|" "$srcdir"/$file > $file
+ done
+}
+
+build() {
+ _configure
+ make world
+}
+
+# Note: (...) instead of {...} is NOT a typo!
+_configure() (
+ export CFLAGS="${CFLAGS/-Os/-O2}"
+ export CPPFLAGS="${CPPFLAGS/-Os/-O2}"
+
+ ./configure \
+ --build=$CBUILD \
+ --host=$CHOST \
+ --prefix=/usr \
+ --mandir=/usr/share/man \
+ --with-system-tzdata=/usr/share/zoneinfo \
+ --with-ldap \
+ --with-libedit-preferred \
+ --with-libxml \
+ --with-openssl \
+ --with-uuid=e2fs \
+ --disable-rpath \
+ --with-perl \
+ --with-python \
+ --with-tcl
+)
+
+check() {
+ cd "$builddir"
+
+ _run_tests src/test
+ _run_tests src/pl
+ _run_tests contrib
+}
+
+check() {
+ cd "$builddir"
+ make check
+}
+
+package() {
+ cd "$builddir"
+
+ make DESTDIR="$pkgdir" install install-docs
+
+ cd "$pkgdir"
+
+ install -d -m750 -o postgres -g postgres \
+ ./var/lib/postgresql \
+ ./var/log/$pkgname
+
+ install -D -m755 "$builddir"/postgresql.initd ./etc/init.d/postgresql
+ install -D -m644 "$builddir"/postgresql.confd ./etc/conf.d/postgresql
+
+ install -D -m755 "$srcdir"/pg-restore.initd ./etc/init.d/pg-restore
+ install -D -m644 "$srcdir"/pg-restore.confd ./etc/conf.d/pg-restore
+}
+
+dev() {
+ default_dev
+
+ _submv usr/bin/pg_config \
+ usr/bin/ecpg \
+ usr/lib/postgresql/pgxs
+}
+
+libpq() {
+ pkgdesc="PostgreSQL libraries"
+ depends=""
+
+ _submv usr/lib/libpq.so.*
+}
+
+libs() {
+ depends=""
+ default_libs
+}
+
+client() {
+ pkgdesc="PostgreSQL client"
+ depends=""
+
+ cd "$pkgdir"/usr/bin
+ mkdir -p "$subpkgdir"/usr/bin
+ mv clusterdb \
+ createdb \
+ createuser \
+ dropdb \
+ dropuser \
+ pg_basebackup \
+ pg_dump \
+ pg_dumpall \
+ pg_isready \
+ pg_receivewal \
+ pg_recvlogical \
+ pg_restore \
+ psql \
+ reindexdb \
+ vacuumdb \
+ "$subpkgdir"/usr/bin/
+}
+
+contrib() {
+ pkgdesc="Extension modules distributed with PostgreSQL"
+ depends=""
+
+ cd "$builddir"
+
+ # Avoid installing plperl and plpython extensions, these will be
+ # installed into separate subpackages.
+ sed -Ei -e 's/(.*_plperl)/#\1/' \
+ -e 's/(.*_plpython)/#\1/' \
+ contrib/Makefile
+
+ make -C contrib DESTDIR="$subpkgdir" install
+
+ mv "$subpkgdir"/usr/share/doc/postgresql/extension \
+ "$pkgdir"/usr/share/doc/postgresql/
+ rmdir -p "$subpkgdir"/usr/share/doc/postgresql || true
+}
+
+pltcl() {
+ pkgdesc="PL/Tcl procedural language for PostgreSQL"
+ depends="pgtcl"
+
+ _submv usr/lib/postgresql/pltcl.so \
+ usr/share/postgresql/extension/pltcl*
+
+ install -m 644 "$srcdir"/pltcl_create_tables.sql \
+ "$subpkgdir"/usr/share/postgresql/
+}
+
+plperl() {
+ pkgdesc="PL/Perl procedural language for PostgreSQL"
+ depends=""
+
+ _submv usr/lib/postgresql/plperl.so \
+ usr/share/postgresql/extension/plperl*
+}
+
+plperl_contrib() {
+ _plcontrib plperl "PL/Perl"
+
+ cd "$builddir"
+ make -C contrib/hstore_plperl DESTDIR="$subpkgdir" install
+}
+
+plpython3() {
+ pkgdesc="PL/Python3 procedural language for PostgreSQL"
+ depends="python3"
+
+ cd "$builddir"
+ make -C src/pl/plpython DESTDIR="$subpkgdir" install
+ rm -R "$subpkgdir"/usr/include
+}
+
+plpython3_contrib() {
+ _plcontrib plpython3 "PL/Python 3"
+
+ cd "$builddir"
+ make -C contrib/hstore_plpython DESTDIR="$subpkgdir" install
+ make -C contrib/ltree_plpython DESTDIR="$subpkgdir" install
+}
+
+_plcontrib() {
+ local subname="$1"
+ pkgdesc="$2 extension modules distributed with PostgreSQL"
+ depends="$pkgname-$subname"
+ install_if="$pkgname-$subname=$pkgver-r$pkgrel $pkgname-contrib=$pkgver-r$pkgrel"
+}
+
+_run_tests() {
+ local path="$1"; shift
+
+ msg "Running test suite at $path..."
+ # Note: some tests fail when running in parallel.
+ make -k -j 1 -C "$path" $@ check MAX_CONNECTIONS=5 || {
+ printf "\n%s\n\n" "Trying to find all regression.diffs files in build directory..." >&2
+ find "$path" -name regression.diffs | while read file; do
+ echo "=== test failure: $file ===" >&2
+ cat "$file" >&2
+ done
+ return 1
+ }
+}
+
+_submv() {
+ local path; for path in "$@"; do
+ mkdir -p "$subpkgdir/${path%/*}"
+ mv "$pkgdir"/$path "$subpkgdir"/${path%/*}/
+ done
+}
+
+sha512sums="88295af13db77a85a604c925aa627d383fdac62c1185119bba87753ce4167a13aed0f055a7a1329b3051f8757c6ba7529baed00a564ef0cfbee685720f282678 postgresql-10.0.tar.bz2
+1f8e7dc58f5b0a12427cf2fd904ffa898a34f23f3332c8382b94e0d991c007289e7913a69e04498f3d93fc5701855796c207b4b1cc4a0b366f586050124d7fcc initdb.patch
+5f9d8bb4957194069d01af8ab3abc6d4d83a7e7f8bd7ebe1caae5361d621a3e58f91b14b952958138a794e0a80bc154fbb7e3e78d211e2a95b9b7901335de854 perl-rpath.patch
+8439a6fdfdea0a4867daeb8bc23d6c825f30c00d91d4c39f48653f5ee77341f23282ce03a77aad94b5369700f11d2cb28d5aee360e59138352a9ab331a9f9d0f conf-unix_socket_directories.patch
+1966b2a3971f56fbecd8daa45965236d487683e13300e5c000e595bbadbbe9d0e1301fbbfa034f452abe8a4e4a427781f772370b9ff392bb03fc11ee43c9dee7 disable-broken-tests.patch
+224e80f9e62843fd248e625abdd0d9fe477729ff3f9a64fc5c86dd37bb7176d3504107fbed7ce578e3a1db7f60b8cf2abf5fe4862c81f76b6d026e29ca495cfc postgresql.initd
+a6d9cba5c7270484b3a22083b2b37742faefb01b6643040050c92235840c601b2e206ebda32804937b729c6cf42c79a558b921900e52fc420df2a03b5f29e1f7 postgresql.confd
+f5a1cba051e7d846c2d16703514601cb25729ed96b677c9bd0c199d64552120a8b14b238af01917fdb87106681e12dee6fff7447558155ba273e4f96be5e2892 pg-restore.initd
+c14a5684e914abb3b0ee71bbf15eed71a9264deacaa404a6e3af6bfc330d93e7598624d0ed11a94263106cc660f7f54c8ff57e759033cf606a795f69ff6c1c7c pg-restore.confd
+5c9bfd9e295dcf678298bf0aa974347a7c311d6e7c2aa76a6920fcb751d01fd1ab77abbec11f3c672f927ad9deaa88e04e370c0b5cd1b60087554c474b748731 pltcl_create_tables.sql"
diff --git a/user/postgresql/conf-unix_socket_directories.patch b/user/postgresql/conf-unix_socket_directories.patch
new file mode 100644
index 000000000..24c51e7b9
--- /dev/null
+++ b/user/postgresql/conf-unix_socket_directories.patch
@@ -0,0 +1,14 @@
+Creating socket in /tmp is silly, but unfortunately it's default location,
+so many clients expect it. Thus we preconfigure PostgreSQL to create socket
+both in /run/postgresql and /tmp.
+--- a/src/backend/utils/misc/postgresql.conf.sample
++++ b/src/backend/utils/misc/postgresql.conf.sample
+@@ -63,7 +63,7 @@
+ #port = 5432 # (change requires restart)
+ #max_connections = 100 # (change requires restart)
+ #superuser_reserved_connections = 3 # (change requires restart)
+-#unix_socket_directories = '/tmp' # comma-separated list of directories
++unix_socket_directories = '/run/postgresql,/tmp' # comma-separated list of directories
+ # (change requires restart)
+ #unix_socket_group = '' # (change requires restart)
+ #unix_socket_permissions = 0777 # begin with 0 to use octal notation
diff --git a/user/postgresql/disable-broken-tests.patch b/user/postgresql/disable-broken-tests.patch
new file mode 100644
index 000000000..0bb5b03fa
--- /dev/null
+++ b/user/postgresql/disable-broken-tests.patch
@@ -0,0 +1,119 @@
+These tests fail due to some really weird linking issue like:
+
+ ERROR: could not load library "<builddir>/tmp_install/usr/lib/postgresql/libpqwalreceiver.so": Error loading shared library libpq.so.5: No such file or directory (needed by <builddir>/tmp_install/usr/lib/postgresql/libpqwalreceiver.so)
+
+psql dynamically loads libpq and libpqwalreceiver libs using pg_dlopen()
+which is an alias for dlopen() on Linux. The above message comes from
+src/backend/utils/fmgr/dfmgr.c. The part "Error loading ..." is from
+pg_dlerror() which is alias for dlerror().
+
+LD_LIBRARY_PATH is set and passed correctly. However, the error
+disappeared when I've copied libpq.so* to /usr/lib...
+
+Also:
+
+ $ LD_LIBRARY_PATH=$(pwd) ldd postgresql/libpqwalreceiver.so
+ ldd (0x280d565e000)
+ libpq.so.5 => <builddir>/tmp_install/usr/lib/libpq.so.5 (0x280d520d000)
+ libc.musl-x86_64.so.1 => ldd (0x280d565e000)
+ libssl.so.43 => /lib/libssl.so.43 (0x280d4fc2000)
+ libcrypto.so.41 => /lib/libcrypto.so.41 (0x280d4c1c000)
+ libldap_r-2.4.so.2 => /usr/lib/libldap_r-2.4.so.2 (0x280d49d6000)
+ liblber-2.4.so.2 => /usr/lib/liblber-2.4.so.2 (0x280d47c9000)
+ libsasl2.so.3 => /usr/lib/libsasl2.so.3 (0x280d45b0000)
+ Error relocating postgresql/libpqwalreceiver.so: appendStringInfoChar: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: MyLatch: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: MemoryContextDelete: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: errstart: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: pg_atoi: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: TupleDescGetAttInMetadata: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: WalReceiverFunctions: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: WaitLatchOrSocket: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: MemoryContextReset: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: pfree: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: errmsg: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: tuplestore_puttuple: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: pchomp: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: appendStringInfoString: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: errfinish: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: pstrdup: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: palloc0: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: errdetail: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: ResetLatch: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: work_mem: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: AllocSetContextCreate: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: CurrentMemoryContext: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: tuplestore_begin_heap: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: BuildTupleFromCStrings: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: errcode: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: MyDatabaseId: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: GetDatabaseEncodingName: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: palloc: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: pg_lsn_in: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: ProcessInterrupts: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: elog_start: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: DirectFunctionCall1Coll: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: elog_finish: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: CreateTemplateTupleDesc: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: InterruptPending: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: TupleDescInitEntry: symbol not found
+ Error relocating postgresql/libpqwalreceiver.so: initStringInfo: symbol not found w
+
+These symbols are really not provided by libpq.
+
+I wasted one day trying to figure out what's going on here, but still
+have no idea. :(
+
+--- a/src/test/regress/parallel_schedule
++++ b/src/test/regress/parallel_schedule
+@@ -84,7 +84,7 @@
+ # ----------
+ # Another group of parallel tests
+ # ----------
+-test: brin gin gist spgist privileges init_privs security_label collate matview lock replica_identity rowsecurity object_address tablesample groupingsets drop_operator password
++test: brin gin gist spgist privileges init_privs security_label collate matview lock replica_identity rowsecurity tablesample groupingsets drop_operator password
+
+ # ----------
+ # Another group of parallel tests
+@@ -98,7 +98,7 @@
+ test: select_parallel
+
+ # no relation related tests can be put in this group
+-test: publication subscription
++test: publication
+
+ # ----------
+ # Another group of parallel tests
+--- a/src/test/modules/dummy_seclabel/Makefile
++++ b/src/test/modules/dummy_seclabel/Makefile
+@@ -6,7 +6,7 @@
+ EXTENSION = dummy_seclabel
+ DATA = dummy_seclabel--1.0.sql
+
+-REGRESS = dummy_seclabel
++#REGRESS = dummy_seclabel
+
+ ifdef USE_PGXS
+ PG_CONFIG = pg_config
+--- a/contrib/postgres_fdw/Makefile
++++ b/contrib/postgres_fdw/Makefile
+@@ -10,7 +10,7 @@
+ EXTENSION = postgres_fdw
+ DATA = postgres_fdw--1.0.sql
+
+-REGRESS = postgres_fdw
++#REGRESS = postgres_fdw
+
+ ifdef USE_PGXS
+ PG_CONFIG = pg_config
+--- a/contrib/dblink/Makefile
++++ b/contrib/dblink/Makefile
+@@ -10,7 +10,7 @@
+ dblink--unpackaged--1.0.sql
+ PGFILEDESC = "dblink - connect to other PostgreSQL databases"
+
+-REGRESS = paths dblink
++#REGRESS = paths dblink
+ REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
+ EXTRA_CLEAN = sql/paths.sql expected/paths.out
+
diff --git a/user/postgresql/initdb.patch b/user/postgresql/initdb.patch
new file mode 100644
index 000000000..59a872a49
--- /dev/null
+++ b/user/postgresql/initdb.patch
@@ -0,0 +1,14 @@
+diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
+--- a/src/bin/initdb/initdb.c
++++ b/src/bin/initdb/initdb.c
+@@ -3259,9 +3259,7 @@
+ /* translator: This is a placeholder in a shell command. */
+ appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
+
+- printf(_("\nSuccess. You can now start the database server using:\n\n"
+- " %s\n\n"),
+- start_db_cmd->data);
++ printf(_("\nSuccess.\n\n"));
+
+ destroyPQExpBuffer(start_db_cmd);
+
diff --git a/user/postgresql/perl-rpath.patch b/user/postgresql/perl-rpath.patch
new file mode 100644
index 000000000..a2505acf7
--- /dev/null
+++ b/user/postgresql/perl-rpath.patch
@@ -0,0 +1,22 @@
+We configure Postgres with --disable-rpath because for the most part we
+want to leave it to ldconfig to determine where libraries are. However,
+for some reason the Perl package puts libperl.so in a nonstandard place
+and doesn't add that place to the ldconfig search path. I think this
+is a Perl packaging bug, myself, but apparently it's not going to change.
+So work around it by adding an rpath spec to plperl.so (only).
+
+Alpine notes:
+This patch is copied from Fedora.
+
+--- a/src/pl/plperl/GNUmakefile
++++ b/src/pl/plperl/GNUmakefile
+@@ -43,6 +43,9 @@
+
+ SHLIB_LINK = $(perl_embed_ldflags)
+
++# Force rpath to be used even though we disable it everywhere else
++SHLIB_LINK += $(rpath)
++
+ REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=plperl --load-extension=plperlu
+ REGRESS = plperl plperl_lc plperl_trigger plperl_shared plperl_elog plperl_util plperl_init plperlu plperl_array
+ # if Perl can support two interpreters in one backend,
diff --git a/user/postgresql/pg-restore.confd b/user/postgresql/pg-restore.confd
new file mode 100644
index 000000000..84a179f6c
--- /dev/null
+++ b/user/postgresql/pg-restore.confd
@@ -0,0 +1,16 @@
+
+# Enable this to dump databases on shutdown and restore on boot
+#
+#PGDUMP="/var/lib/postgresql/backup/databases.pgdump"
+
+# The dump file will be deleted once restored unless KEEP_DUMP is set.
+# This is to avoid accidental restoring of running database. If you know what
+# you are doing and want keep the dump, then enable the KEEP_DUMP option
+# below.
+#
+#KEEP_DUMP=yes
+
+# If you enable KEEP_DUMP above you probably also want the --clean option
+# the the pg_dumpall command.
+#PG_DUMPALL_OPTS="--clean"
+
diff --git a/user/postgresql/pg-restore.initd b/user/postgresql/pg-restore.initd
new file mode 100644
index 000000000..e9fe65c19
--- /dev/null
+++ b/user/postgresql/pg-restore.initd
@@ -0,0 +1,36 @@
+#!/sbin/openrc-run
+
+extra_commands="${opts} dump restore purge"
+
+depend() {
+ need postgresql
+}
+
+restore() {
+ yesno "$PGDUMP" && return 0
+ ebegin "Restoring PostgreSQL $PGDUMP"
+ psql -U ${PG_USER:-postgres} ${PSQL_OPTS} -f "$PGDUMP" >/dev/null 2>/dev/null
+ local res=$?
+ yesno "$KEEP_DUMP" || rm -f "$PGDUMP"
+ eend $res
+ /etc/init.d/postgresql reload
+}
+
+dump() {
+ yesno "$PGDUMP" && return 0
+ mkdir -p "$( dirname "$PGDUMP" )"
+ ebegin "Saving PostgreSQL databases to $PGDUMP"
+ pg_dumpall -U ${PG_USER:-postgres} ${PG_DUMPALL_OPTS} -f "$PGDUMP"
+ eend $?
+}
+
+purge() {
+ yesno "$PGDUMP" && return 0
+ ebegin "Removing temporary $PGDUMP PostgreSQL backup"
+ rm -f "$PGDUMP"
+ eend $?
+}
+
+start() {
+ restore
+}
diff --git a/user/postgresql/pltcl_create_tables.sql b/user/postgresql/pltcl_create_tables.sql
new file mode 100644
index 000000000..76c7a954a
--- /dev/null
+++ b/user/postgresql/pltcl_create_tables.sql
@@ -0,0 +1,13 @@
+-- Create tables needed for PL/Tcl autoloading. This script should be run by
+-- the database administrator only.
+--
+-- Statements in this script are extracted from pltcl_loadmod script.
+--
+-- Author: G.J.R. Timmer
+-- Date: 2017-01-28
+
+create table pltcl_modules (modname name, modseq int2, modsrc text);
+create index pltcl_modules_i on pltcl_modules using btree (modname name_ops);
+
+create table pltcl_modfuncs (funcname name, modname name);
+create index pltcl_modfuncs_i on pltcl_modfuncs using hash (funcname name_ops);
diff --git a/user/postgresql/postgresql.confd b/user/postgresql/postgresql.confd
new file mode 100644
index 000000000..6a25cc381
--- /dev/null
+++ b/user/postgresql/postgresql.confd
@@ -0,0 +1,59 @@
+# Which port and socket to bind PostgreSQL.
+# This may be overriden in postgresql.conf.
+#port="5432"
+
+# How long to wait for server to start in seconds.
+#start_timeout=10
+
+# Number of seconds to wait for clients to disconnect from the server before
+# shutting down. Set to zero to disable this timeout.
+#nice_timeout=60
+
+# Timeout in seconds for rude quit - forecfully disconnect clients from server
+# and shut down. This is performed after nice_timeout exceeded. Terminated
+# client connections have their open transactions rolled back.
+# Set "rude_quit=no" to disable.
+#rude_quit="yes"
+#rude_timeout=30
+
+# Timeout in seconds for force quit - if the server still fails to shutdown,
+# you can force it to quit and a recover-run will execute on the next startup.
+# Set "force_quit=yes" to enable.
+#force_quit="no"
+#force_timeout="2"
+
+# Extra options to run postmaster with, e.g.:
+# -N is the maximal number of client connections
+# -B is the number of shared buffers (has to be at least 2x the value for -N)
+# Please read man postgres(1) for more options. Many of these options can be
+# set directly in the configuration file.
+#pg_opts="-N 512 -B 1024"
+
+# Pass extra environment variables. If you have to export environment variables
+# for the database process, this can be done here.
+# Don't forget to escape quotes.
+#env_vars="PGPASSFILE=\"/path/to/.pgpass\""
+
+# Location of postmaster.log. Default is $data_dir/postmaster.log.
+logfile="/var/log/postgresql/postmaster.log"
+
+# Automatically set up a new database if missing on startup.
+#auto_setup="yes"
+
+
+##############################################################################
+#
+# The following values should NOT be arbitrarily changed!
+#
+# The initscript uses these variables to inform PostgreSQL where to find
+# its data directory and configuration files.
+
+# Where the data directory is located/to be created.
+#data_dir="/var/lib/postgresql/@VERSION@/data"
+
+# Location of configuration files. Default is $data_dir.
+conf_dir="/etc/postgresql"
+
+# Additional options to pass to initdb.
+# See man initdb(1) for available options.
+#initdb_opts="--locale=en_US.UTF-8"
diff --git a/user/postgresql/postgresql.initd b/user/postgresql/postgresql.initd
new file mode 100644
index 000000000..846229a40
--- /dev/null
+++ b/user/postgresql/postgresql.initd
@@ -0,0 +1,220 @@
+#!/sbin/openrc-run
+
+extra_started_commands="reload"
+description_reload="Reload configuration"
+
+extra_stopped_commands="setup"
+description_setup="Initialize a new PostgreSQL cluster"
+
+# Note: Uppercase variables are here for backward compatibility.
+
+: ${user:=${PGUSER:-"postgres"}}
+: ${group:=${PGGROUP:-"postgres"}}
+
+: ${auto_setup:=${AUTO_SETUP:-"yes"}}
+: ${start_timeout:=${START_TIMEOUT:-10}}
+: ${nice_timeout:=${NICE_TIMEOUT:-60}}
+: ${rude_quit:=${RUDE_QUIT:-"yes"}}
+: ${rude_timeout:=${RUDE_TIMEOUT:-30}}
+: ${force_quit:=${FORCE_QUIT:-"no"}}
+: ${force_timeout:=${FORCE_TIMEOUT:-2}}
+
+: ${data_dir:=${PGDATA:-"/var/lib/postgresql/@VERSION@/data"}}
+: ${conf_dir:=$data_dir}
+: ${env_vars:=${PG_EXTRA_ENV:-}}
+: ${initdb_opts:=${PG_INITDB_OPTS:-}}
+: ${logfile:="$data_dir/postmaster.log"}
+: ${pg_opts:=${PGOPTS:-}}
+: ${port:=${PGPORT:-5432}}
+
+command="/usr/bin/postgres"
+
+conffile="$conf_dir/postgresql.conf"
+pidfile="$data_dir/postmaster.pid"
+start_stop_daemon_args="
+ --user $user
+ --group $group
+ --pidfile $pidfile
+ --wait 100"
+
+depend() {
+ use net
+ after firewall
+
+ if [ "$(get_config log_destination)" = "syslog" ]; then
+ use logger
+ fi
+}
+
+start_pre() {
+ check_deprecated_var WAIT_FOR_START start_timeout
+ check_deprecated_var WAIT_FOR_DISCONNECT nice_timeout
+ check_deprecated_var WAIT_FOR_CLEANUP rude_timeout
+ check_deprecated_var WAIT_FOR_QUIT force_timeout
+
+ if [ ! -d "$data_dir/base" ]; then
+ if yesno "$auto_setup"; then
+ setup || return 1
+ else
+ eerror "Database not found at: $data_dir"
+ eerror "Please make sure that 'data_dir' points to the right path."
+ eerror "You can run '/etc/init.d/postgresql setup' to setup a new database cluster."
+ return 1
+ fi
+ fi
+
+ local socket_dirs=$(get_config "unix_socket_directories" "/run/postgresql")
+ local port=$(get_config "port" "$port")
+
+ start_stop_daemon_args="$start_stop_daemon_args --env PGPORT=$port"
+
+ (
+ # Set the proper permission for the socket paths and create them if
+ # then don't exist.
+ set -f; IFS=","
+ for dir in $socket_dirs; do
+ if [ -e "${dir%/}/.s.PGSQL.$port" ]; then
+ eerror "Socket conflict. A server is already listening on:"
+ eerror " ${dir%/}/.s.PGSQL.$port"
+ eerror "Hint: Change 'port' to listen on a different socket."
+ return 1
+ elif [ "${dir%/}" != "/tmp" ]; then
+ checkpath -d -m 1775 -o $user:$group "$dir"
+ fi
+ done
+ )
+}
+
+start() {
+ local retval
+
+ ebegin "Starting PostgreSQL"
+
+ local var; for var in $env_vars; do
+ start_stop_daemon_args="$start_stop_daemon_args --env $var"
+ done
+
+ rm -f "$pidfile"
+ start-stop-daemon --start \
+ $start_stop_daemon_args \
+ --exec /usr/bin/pg_ctl \
+ -- start \
+ --silent \
+ -w --timeout="$start_timeout" \
+ --log="$logfile" \
+ --pgdata="$conf_dir" \
+ -o "--data-directory=$data_dir $pg_opts"
+ retval=$?
+
+ if [ $retval -ne 0 ]; then
+ eerror "Check the log for a possible explanation of the above error:"
+ eerror " $logfile"
+ fi
+ eend $retval
+}
+
+stop() {
+ local retry="SIGTERM/$nice_timeout"
+
+ yesno "$rude_quit" \
+ && retry="$retry/SIGINT/$rude_timeout" \
+ || rude_timeout=0
+
+ yesno "$force_quit" \
+ && retry="$retry/SIGQUIT/$force_timeout" \
+ || force_timeout=0
+
+ local seconds=$(( $nice_timeout + $rude_timeout + $force_timeout ))
+
+ ebegin "Stopping PostgreSQL (this can take up to $seconds seconds)"
+
+ start-stop-daemon --stop \
+ --exec "$command" \
+ --retry "$retry" \
+ --progress \
+ --pidfile "$pidfile"
+ eend $?
+}
+
+reload() {
+ ebegin "Reloading PostgreSQL configuration"
+
+ start-stop-daemon --signal HUP --pidfile "$pidfile"
+ eend $?
+}
+
+setup() {
+ local bkpdir
+
+ ebegin "Creating a new PostgreSQL database cluster"
+
+ if [ -d "$data_dir/base" ]; then
+ eend 1 "$data_dir/base already exists!"; return 1
+ fi
+
+ # If data_dir exists, backup configs.
+ if [ -d "$data_dir" ]; then
+ bkpdir="$(mktemp -d)"
+ find "$data_dir" -type f -name "*.conf" -maxdepth 1 \
+ -exec mv -v {} "$bkpdir"/ \;
+ rm -rf "$data_dir"/*
+ fi
+
+ install -d -m 0700 -o $user -g $group "$data_dir"
+ install -d -m 0750 -o $user -g $group "$conf_dir"
+
+ cd "$data_dir" # to avoid the: could not change directory to "/root"
+ su $user -c "/usr/bin/initdb $initdb_opts --pgdata $data_dir"
+ local retval=$?
+
+ if [ -d "$bkpdir" ]; then
+ # Move backuped configs back.
+ mv -v "$bkpdir"/* "$data_dir"/
+ rm -rf "$bkpdir"
+ fi
+
+ if [ "${data_dir%/}" != "${conf_dir%/}" ]; then
+ # Move configs from data_dir to conf_dir and symlink them to data_dir.
+ local name newname
+ for name in postgresql.conf pg_hba.conf pg_ident.conf; do
+ newname="$name"
+ [ ! -e "$conf_dir"/$name ] || newname="$name.new"
+
+ mv "$data_dir"/$name "$conf_dir"/$newname
+ ln -s "$conf_dir"/$name "$data_dir"/$name
+ done
+ fi
+
+ eend $retval
+}
+
+
+get_config() {
+ local name="$1"
+ local default="${2:-}"
+
+ if [ ! -f "$conffile" ]; then
+ printf '%s\n' "$default"
+ return 1
+ fi
+ sed -En "/^\s*${name}\b/{ # find line starting with the name
+ s/^\s*${name}\s*=?\s*([^#]+).*/\1/; # capture the value
+ s/\s*$//; # trim trailing whitespaces
+ s/^['\"](.*)['\"]$/\1/; # remove delimiting quotes
+ p
+ }" "$conffile" \
+ | grep . || printf '%s\n' "$default"
+}
+
+check_deprecated_var() {
+ local old_name="$1"
+ local new_name="$2"
+
+ if [ -n "$(getval "$old_name")" ]; then
+ ewarn "Variable '$old_name' has been removed, please use '$new_name' instead."
+ fi
+}
+
+getval() {
+ eval "printf '%s\n' \"\$$1\""
+}
diff --git a/user/postgresql/postgresql.pre-upgrade b/user/postgresql/postgresql.pre-upgrade
new file mode 100644
index 000000000..6116252c1
--- /dev/null
+++ b/user/postgresql/postgresql.pre-upgrade
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+new="$1"
+old="$2"
+
+pgver=${new%.*}
+compare=$(apk version -t "$old" $pgver)
+
+# check if we upgrade from earlier than $pgver and if it is running
+if [ "$compare" != "<" ] || ! /etc/init.d/postgresql --quiet status; then
+ exit 0
+fi
+
+if [ -f /etc/conf.d/postgresql ]; then
+ . /etc/conf.d/postgresql
+fi
+
+cat <<EOF
+* You are upgrading to postgres $pgver wich is not compatible with the running
+* See: http://www.postgresql.org/docs/$pgver/static/upgrading.html
+*
+* The corresponding steps for Alpine Linux is:
+*
+* pg_dumpall -U ${PGUSER:-postgres} > dumpfile
+* /etc/init.d/postgresql stop
+* apk add -u postgresql
+* /etc/init.d/postgresql setup
+* /etc/init.d/postgresql start
+* psql -U ${PGUSER:-postgres} -f dumpfile
+*
+EOF
+exit 1