@@ -85,16 +85,11 @@ Options:
85
85
string). When no UNIT_NAME is explicitly passed,
86
86
the 'postgresql' string is used by default.
87
87
--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.
98
93
--upgrade-from-unit=UNIT Select proper unit name to upgrade from. This
99
94
has similar semantics as --unit option.
100
95
--upgrade-ids Print list of available IDs of upgrade scenarios to
@@ -401,6 +396,37 @@ handle_service_env()
401
396
done
402
397
}
403
398
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
+ }
404
430
405
431
handle_envfile ()
406
432
{
@@ -418,6 +444,7 @@ handle_envfile()
418
444
# Note that the env file parser in systemd does not perform exactly the
419
445
# same job.
420
446
unset PGPORT PGDATA
447
+ # Source the file, loading the variables in it
421
448
. " $file "
422
449
envfile_pgdata=" $PGDATA "
423
450
envfile_pgport=" $PGPORT "
@@ -503,18 +530,35 @@ service_configuration()
503
530
&& set_var " $datavar " " @PGDATADIR@"
504
531
handle_envfile " @initscriptsconfdir@/$service "
505
532
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
511
550
fi
512
551
513
552
# EnvironmentFile beats Environment configuration in systemd. In sysvinit
514
553
# there is no "unit_pgdata". So make sure the envfile_gpdata is used later
515
554
# than unit_pgdata.
516
555
test -n " $unit_pgdata " && set_var " $datavar " " $unit_pgdata "
517
556
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
518
562
519
563
# skip for the first run
520
564
test initdb = " $mode " && return
@@ -571,6 +615,7 @@ option_service="@NAME_SERVICE@"
571
615
option_port=
572
616
option_debug=0
573
617
option_upgradefrom_unit=
618
+ option_datadir=
574
619
575
620
# Content of EnvironmentFile= files fills those:
576
621
envfile_pgdata=
@@ -626,10 +671,9 @@ while true; do
626
671
shift 2
627
672
;;
628
673
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
633
677
;;
634
678
635
679
--debug)
@@ -698,7 +742,7 @@ debug "service name: $option_service"
698
742
service_configuration initdb pgdata UNUSED " $option_service "
699
743
700
744
test " $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 "
702
746
703
747
[[ " $pgdata " =~ ^/.* ]] \
704
748
|| die $" the PostgreSQL datadir not absolute path: '$pgdata ', try --debug"
775
819
776
820
# # LAST CHECK THE SETUP ##
777
821
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
779
826
780
827
# These variables are read by underlying utilites, rather export them.
781
828
export PGDATA=$pgdata
0 commit comments