Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eroGk-0000EL-SE for pgsql-admin@arkaria.postgresql.org; Fri, 02 Mar 2018 17:14:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eroGj-0000nD-Lf for pgsql-admin@arkaria.postgresql.org; Fri, 02 Mar 2018 17:14:53 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eroGj-0000n4-Av for pgsql-admin@lists.postgresql.org; Fri, 02 Mar 2018 17:14:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eroGg-0006fK-K4 for pgsql-admin@lists.postgresql.org; Fri, 02 Mar 2018 17:14:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w22HEnjZ031669; Fri, 2 Mar 2018 12:14:49 -0500 From: Tom Lane To: Mike Lonergan cc: pgsql-admin@lists.postgresql.org Subject: Re: What is the accepted practice to automate initdb (PostgreSQL 9.6) to a non-default directory? In-reply-to: References: Comments: In-reply-to Mike Lonergan message dated "Fri, 02 Mar 2018 09:00:35 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31667.1520010889.1@sss.pgh.pa.us> Date: Fri, 02 Mar 2018 12:14:49 -0500 Message-ID: <31668.1520010889@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mike Lonergan writes: > The internet has the following pattern documented in many places (with > varying install locations for "initdb", but always the same command) > through at least PostgreSQL 9.3: > sudo -u postgres postgres /opt/pg/bin/initdb -D /data/ > However while this command can complete successfully (which is encouraging, > as it's still documented in the 9.6 docs > ), running sudo > postgresql service start results in: > Redirecting to /bin/systemctl start postgresql.service > Job for postgresql.service failed because the control process exited > with error code. See "systemctl status postgresql.service" and > "journalctl -xe" for details. Evidently you're relying on somebody's systemd unit script to launch Postgres. Almost certainly, that unit script has the location of the data directory hard-wired into it. If you want to use a non-default directory location, you'll have to change the unit script (and possibly then mutter some incantation to get systemd to notice you've changed it; I've not worked with systemd in awhile so I forget). > The recommended command "postgresql-setup --initdb" doesn't seem to accept > -D or --pgdata parameters, so that seems a non-starter for my scenario. While it's certainly possible to use a manual invocation of initdb to get things going, when your distro provides a wrapper like postgresql-setup you're better off using that, because it's certain to get the data directory location, ownership, etc set up the way the launch script expects. The reason there's no -D option is that it scrapes the datadir location out of the launch script; again, you *must* change that script if you're going to use this launch method. > I've seen various unofficial solutions that use various combinations of > manual setup and hand-editing of .conf files, and I'm trying hard to avoid > that so that my ops team can build and rebuild this server in an emergency > situation without any special knowledge of the behaviour of PostgreSQL or > its tools. If that's what you're striving for, then the very first recommendation would be to *not* use a non-default data dir location, but just go along with what the distro wants to do out-of-the-box. You're just adding another layer of complexity and things-to-go-wrong. (An example of the sort of thing I'm worried about is that you're likely also going to need to have a conversation with SELinux about whether the postgres daemon is allowed to use files in the nonstandard location. It's doable, certainly, but it's one more thing to configure.) regards, tom lane