[Service]
Environment=PGDATA=/srv/postgresql/postgresql
Environment=PG_CONF=/etc/postgresql/postgresql.conf
Environment=PG_HBA=/etc/postgresql/pg_hba.conf
Environment=PG_IDENT=/etc/postgresql/pg_ident.conf
Environment=LC_ALL=fr_FR.UTF-8
ExecStartPre=
ExecStartPre=+/usr/local/lib/sbin/postgresql_init
# if upgrade needed, do it
ExecStartPre=/bin/bash -c '%slurp
/usr/libexec/postgresql-check-db-dir %N || (%slurp
 echo "UPGRADE" &&%slurp
# directory creation must have 700 rights
 umask 0077 &&%slurp
# pg_upgrade do not like ssl activation
 /bin/grep -v "ssl " ${PG_CONF} > /tmp/postgresql.conf &&%slurp
 mv -f /tmp/postgresql.conf ${PGDATA}/postgresql.conf &&%slurp
# pg_upgrade modify pg_hba.conf so copy it
 /bin/rm ${PGDATA}/pg_hba.conf &&%slurp
 /bin/cp -af ${PG_HBA} ${PGDATA} &&%slurp
# do upgrade
 /usr/bin/postgresql-setup --upgrade &&%slurp
# re do link
 ln -sf ${PG_HBA} ${PGDATA}/ &&%slurp
 ln -sf ${PG_CONF} ${PGDATA}/ &&%slurp
# remove old cluster
 /srv/postgresql/postgresql/delete_old_cluster.sh &&%slurp
 rm -f /srv/postgresql/postgresql/delete_old_cluster.sh &&%slurp
# force index (see later)
 touch /srv/postgresql/risotto_upgrade.lock%slurp
)'
# recheck db
ExecStartPre=/usr/libexec/postgresql-check-db-dir %N
ExecStart=
ExecStart=/usr/bin/postmaster -D ${PGDATA} -c config_file=${PG_CONF} -c hba_file=${PG_HBA} -c ident_file=${PG_IDENT}
ExecStartPost=-/usr/bin/psql -f /etc/postgresql/postgresql.sql
# if lock do reindex
ExecStartPost=/bin/bash -c 'if [ -f /srv/postgresql/risotto_upgrade.lock ];then echo REINDEX; /usr/bin/reindexdb && rm -f /srv/postgresql/risotto_upgrade.lock; fi'