25

i was installing postgresql on ubuntu using linuxbrew:

brew install postgresql

it seems to work fine but after that because i was installing PostgreSQL for the first time i tried creating a database:

initdb /usr/local/var/postgres -E utf8

but it returned as:

initdb: command not found

i tried running the command with sudo but that doesn't helped

6 Answers 6

24

run locate initdb it should give you the list to chose. smth like:

MacBook-Air:~ vao$ locate initdb
/usr/local/Cellar/postgresql/9.5.3/bin/initdb
/usr/local/Cellar/postgresql/9.5.3/share/doc/postgresql/html/app-initdb.html
/usr/local/Cellar/postgresql/9.5.3/share/man/man1/initdb.1
/usr/local/Cellar/postgresql/9.6.1/bin/initdb
/usr/local/Cellar/postgresql/9.6.1/share/doc/postgresql/html/app-initdb.html
/usr/local/Cellar/postgresql/9.6.1/share/man/man1/initdb.1
/usr/local/bin/initdb
/usr/local/share/man/man1/initdb.1

So in my case I want to run

/usr/local/Cellar/postgresql/9.6.1/bin/initdb 

If you don't have mlocate installed, either install it or use

sudo find / -name initdb
Sign up to request clarification or add additional context in comments.

Comments

17

There's a good answer to a similar question on SuperUser.

In short:

  1. Postgres groups databases into "clusters", each of which is a named collection of databases sharing a configuration and data location, and running on a single server instance with its own TCP port.
  2. If you only want a single instance of Postgres, the installation includes a cluster named "main", so you don't need to run initdb to create one.
  3. If you do need multiple clusters, then the Postgres packages for Debian and Ubuntu provide a different command pg_createcluster to be used instead of initdb, with the latter not included in PATH so as to discourage end users from using it directly.

And if you're just trying to create a database, not a database cluster, use the createdb command instead.

Comments

15

I had the same problem and found the answer here.

Ubuntu path is

/usr/lib/postgresql/9.6/bin/initdb

Edit: Sorry, Ahmed asked about linuxbrew, I'm talking about Ubuntu. I Hope this answer helps somebody.

Comments

5

I had a similar issue caused by the brew install postgresql not properly linking postgres. The solve for me was to run:

brew link --overwrite postgresql

Comments

2

if you installed postgresql and got initdb: command not found

you should add below line in ~/.bashrc (OR .zshrc) file:

export PATH=$PATH:/usr/lib/postgresql/x.y/bin/

and then

source ~/.bashrc

Actually the initdb is exist in that path. after that you can use initdb command.

Comments

0

you can add the PATH to run from any location

sudo nano ~/.profile

inside nano go to the end and add the following

# set PATH so it includes user's private bin if it exists
if [ -d "/usr/lib/postgresql/14/bin/" ] ; then
    PATH="/usr/lib/postgresql/14/bin/:$PATH"
fi

and configure the alternative

sudo update-alternatives --install /usr/bin/initdb initdb /usr/lib/postgresql/14/bin/initdb 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.