@@ -85,16 +85,11 @@ Options:
8585 string). When no UNIT_NAME is explicitly passed,
8686 the 'postgresql' string is used by default.
8787 --port=PORT port where the initialized server will listen for
88- connections"
89-
90- test 0 -eq @WANT_SYSVINIT@ && \
91- USAGE_STRING+="
92- --new-systemd-unit We dropped this option for security reasons.
93- Nowadays, please use the root-only script
94- @sbindir@/@NAME_BINARYBASE@-new-systemd-unit.
95- --datadir Dropped with --new-systemd-unit."
96-
97- USAGE_STRING+="
88+ connections
89+ --datadir Override automatic detection of PostgreSQL data
90+ directory. If your system is using systemd as init
91+ system, it is advisable to change data directory
92+ path in service file instead.
9893 --upgrade-from-unit=UNIT Select proper unit name to upgrade from. This
9994 has similar semantics as --unit option.
10095 --upgrade-ids Print list of available IDs of upgrade scenarios to
@@ -401,6 +396,37 @@ handle_service_env()
401396 done
402397}
403398
399+ handle_service_file ()
400+ {
401+ local service_file=" $1 "
402+ local line var_name var_value
403+ debug " Parsing ${service_file} "
404+ local systemd_env=" $( cat " ${service_file} " ) " \
405+ || { return ; }
406+
407+ while IFS= read -r line; do
408+ # Only lines starting with Environment=
409+ [[ " $line " =~ ^Environment= ]] || continue
410+
411+ # Remove 'Environment=' prefix
412+ line=" ${line# Environment=} "
413+
414+ for env_val in $line ; do
415+ var_name=" ${env_val%% =* } "
416+ var_value=" ${env_val#* =} "
417+ debug " Found environment variable: $var_name =$var_value "
418+
419+ case " $var_name " in
420+ PGDATA)
421+ unit_pgdata=" $var_value "
422+ ;;
423+ PGPORT)
424+ unit_pgport=" $var_value "
425+ ;;
426+ esac
427+ done
428+ done <<< " $systemd_env"
429+ }
404430
405431handle_envfile ()
406432{
@@ -418,6 +444,7 @@ handle_envfile()
418444 # Note that the env file parser in systemd does not perform exactly the
419445 # same job.
420446 unset PGPORT PGDATA
447+ # Source the file, loading the variables in it
421448 . " $file "
422449 envfile_pgdata=" $PGDATA "
423450 envfile_pgport=" $PGPORT "
@@ -503,18 +530,35 @@ service_configuration()
503530 && set_var " $datavar " " @PGDATADIR@"
504531 handle_envfile " @initscriptsconfdir@/$service "
505532 else
506- # We ship two service files, @[email protected] and 507- # @NAME_SERVICE@@.service. The former has PGDATA set by default
508- # similarly to sysvinit case.
509- handle_service_env " $service "
510- handle_service_envfiles " $option_mode " " $service "
533+ if grep -q systemd /proc/1/comm; then
534+ # If booted with systemd as PID 1, we try to find the variables
535+ # using systemctl show -p Environment= @[email protected] 536+ # We ship two service files, @[email protected] and 537+ # @NAME_SERVICE@@.service. The former has PGDATA set by default
538+ # similarly to sysvinit case.
539+ debug " System booted with systemd as PID 1, using systemctl to find" \
540+ " service configuration for $service "
541+ handle_service_env " $service "
542+ handle_service_envfiles " $option_mode " " $service "
543+ else
544+ # If not booted with systemd, we try to find the service file in
545+ # predefined path and parse it manually.
546+ warn " System not booted with systemd as PID 1. Manually parsing service" \
547+ " file @INIT_SYSTEM_SERVICE_PATH@/$service .service"
548+ handle_service_file " @INIT_SYSTEM_SERVICE_PATH@/$service .service"
549+ fi
511550 fi
512551
513552 # EnvironmentFile beats Environment configuration in systemd. In sysvinit
514553 # there is no "unit_pgdata". So make sure the envfile_gpdata is used later
515554 # than unit_pgdata.
516555 test -n " $unit_pgdata " && set_var " $datavar " " $unit_pgdata "
517556 test -n " $envfile_pgdata " && set_var " $datavar " " $envfile_pgdata "
557+ # If the user specified --datadir, take priority over all
558+ if [ -n " ${option_datadir} " ]; then
559+ info $" Using datadir from --datadir: $option_datadir "
560+ set_var " $datavar " " $option_datadir "
561+ fi
518562
519563 # skip for the first run
520564 test initdb = " $mode " && return
@@ -571,6 +615,7 @@ option_service="@NAME_SERVICE@"
571615option_port=
572616option_debug=0
573617option_upgradefrom_unit=
618+ option_datadir=
574619
575620# Content of EnvironmentFile= files fills those:
576621envfile_pgdata=
@@ -626,10 +671,9 @@ while true; do
626671 shift 2
627672 ;;
628673
629- --datadir|--new-systemd-unit)
630- error $" Removed option --new-systemd-unit/--datadir, please use"
631- error_q $" @sbindir@/@NAME_BINARYBASE@-new-systemd-unit script"
632- exit 1
674+ --datadir)
675+ option_datadir=" $2 "
676+ shift 2
633677 ;;
634678
635679 --debug)
@@ -698,7 +742,7 @@ debug "service name: $option_service"
698742service_configuration initdb pgdata UNUSED " $option_service "
699743
700744test " $pgdata " = default \
701- && die $" no db datadir (PGDATA) configured for ' $option_service$srvsuff ' unit "
745+ && die $" no db datadir (PGDATA) found, try using --datadir option "
702746
703747[[ " $pgdata " =~ ^/.* ]] \
704748 || die $" the PostgreSQL datadir not absolute path: '$pgdata ', try --debug"
775819
776820# # LAST CHECK THE SETUP ##
777821
778- check_daemon_reload
822+ if grep -q systemd /proc/1/comm; then
823+ # Check only if we are running under systemd.
824+ check_daemon_reload
825+ fi
779826
780827# These variables are read by underlying utilites, rather export them.
781828export PGDATA=$pgdata
0 commit comments