21

I'm following these instructions, however I can only get to step 17.2.

Despite installing postgresql successfully via the

sudo apt-get install postgresql

command, upon running

initdb -D /usr/local/pgsql/data

Ubuntu tells me that it 'initdb' isn't installed. The instructions tell me this command is installed by

sudo apt-get install postgresql

so what's going on? I can make initdb available by installing postgres-xc, but I think postgres-xc is just some weird third party rubbish, and it's not detailed in the instructions. Any ideas?

Starkers
  • 3,029
  • 13
  • 30
  • 40

4 Answers4

32

You will find initdb under /usr/lib/postgresql/x.y/bin/. See also /usr/share/doc/postgresql-common/README.Debian.gz for more information on the setup on Debian and Ubuntu.

Peter Eisentraut
  • 1,687
  • 12
  • 8
  • This really should be the accepted answer. `initdb` is the underlying command but Debian and Ubuntu users should be using pg_createcluster and its related suite of commands. Furthermore you normally do not need to `initdb` OR `pg_createcluster` after `apt-get install postgresql` because the standard install already creates a default cluster, with a server and default/template databases, for you. The README Peter mentions above is worth your time to read. – cdaddr May 14 '14 at 07:52
  • 2
    @cdaddr No, you usually don't need it after installing postgres. Still, if you find yourself in need of recreating a cluster quickly and don't want to bother with reinstalling postgres or if you need to initialise a new database in a non-standard location this may come in handy. So yes, this is a great answer. – Erathiel Nov 09 '15 at 10:12
  • 2
    Works. And it's the best because this way I can be sure which version I'm using. – sudo Jul 07 '17 at 22:17
  • we are searching for initdb because, tutorial says so https://www.postgresql.org/docs/10/static/creating-cluster.html – nerkn Dec 08 '17 at 08:05
  • ... but: `ls -l /usr/lib/postgresql/11/bin/` returns: ` total 36 -rwxr-xr-x 1 root root 34888 Aug 13 23:56 pg_config*` and running `pg_createcluster ...args` returns: ` Error: no initdb program for version 11 found ` So the package appears to be broke. – David Nugent Jan 04 '21 at 22:21
16

initdb is intended to be run under the postgres user account that is created during the install. After installing postgresql you can do:

sudo su - postgres

Then you should be able to run initdb.

Brian.D.Myers
  • 600
  • 8
  • 16
  • 11
    Quoting from /usr/share/doc/postgresql-common/README.Debian.gz: `Please note that you can of course also use the upstream tools for creating clusters, such as initdb(1). However, please note that in this case you cannot expect *any* of above pg_* tools to work, since they use different configuration settings and file locations. If in doubt, then do *not* use initdb, but only pg_createcluster. Since merely installing postgresql-X.Y will already set up a default cluster which is ready to work, most people do not need to bother about initdb or pg_createcluster at all.` – cdaddr May 14 '14 at 07:56
  • 4
    Even after switching to the postgres user I still get this error – Cogwheel May 13 '17 at 19:04
  • This doesn't work for me in Ubuntu Server 14 using Postgres 9.6. – sudo Jul 07 '17 at 22:17
  • 1
    It does not work, since `initdb` is in `/usr/lib/postgresql/X.X/bin/`. – Li Dong Nov 15 '17 at 09:07
  • Unfortunately this is an artificial limitation introduced by the Debian package, and the documentation FUD describes this. Note that `pg_createcluster` is another Debian-ism which seems to be absent in newer versions. – Vladimir Panteleev Jul 13 '23 at 13:28
2

initdb is not installed as user executable. Is only installed in /usr/lib/postgresql/X.X/bin/, because it always depends on the version. initdb can only be executed from that specific directory.

As mentioned in other answers, installation of postgres creates a default directory that may be in a limited partition. Users may want to change this, but it requires other steps also. see here.

ilias iliadis
  • 361
  • 1
  • 2
  • 9
-2

Follow the following steps with user root

  1. passwd postgres - your password
  2. su postgres
  3. psql
  4. Create a user with your user name like CREATE USER SAM ;
  5. create database sam;
  6. Log out and type psql <your_user>
EAmez
  • 103
  • 5
smn_onrocks
  • 526
  • 5
  • 14