From 0730e81785e3bd4342df9c53d07618659b8249a7 Mon Sep 17 00:00:00 2001
From: Danylo Hlynskyi <abcz2.uprola@gmail.com>
Date: Wed, 7 Aug 2019 14:17:36 +0300
Subject: [PATCH] postgresql: running `initdb` from command line now works
 (#65309)

The issue was only with NixOS service, `postgresql` installed through
`nix-env` was not affected.

Fixes https://github.com/NixOS/nixpkgs/issues/23655
---
 .../modules/services/databases/postgresql.nix |  4 +++
 nixos/tests/initdb.nix                        | 26 +++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 nixos/tests/initdb.nix

diff --git a/nixos/modules/services/databases/postgresql.nix b/nixos/modules/services/databases/postgresql.nix
index 37d44e30fbeb..7ff899970ccd 100644
--- a/nixos/modules/services/databases/postgresql.nix
+++ b/nixos/modules/services/databases/postgresql.nix
@@ -251,6 +251,10 @@ in
 
     environment.systemPackages = [ postgresql ];
 
+    environment.pathsToLink = [
+     "/share/postgresql"
+    ];
+
     systemd.services.postgresql =
       { description = "PostgreSQL Server";
 
diff --git a/nixos/tests/initdb.nix b/nixos/tests/initdb.nix
new file mode 100644
index 000000000000..749d7857a134
--- /dev/null
+++ b/nixos/tests/initdb.nix
@@ -0,0 +1,26 @@
+let
+  pkgs = import <nixpkgs> { };
+in
+with import <nixpkgs/nixos/lib/testing.nix> { inherit pkgs; system = builtins.currentSystem; };
+with pkgs.lib;
+
+makeTest {
+    name = "pg-initdb";
+
+    machine = {...}:
+      {
+        documentation.enable = false;
+        services.postgresql.enable = true;
+        services.postgresql.package = pkgs.postgresql_9_6;
+        environment.pathsToLink = [
+          "/share/postgresql"
+        ];
+      };
+
+    testScript = ''
+      $machine->start;
+      $machine->succeed("sudo -u postgres initdb -D /tmp/testpostgres2");
+      $machine->shutdown;
+    '';
+
+  }
\ No newline at end of file