36

I'm following the recent RailsCast on setting up PostgreSQL, but I'm unable to run the initdb /usr/local/var/postgres command. Each time I run it, I get this error:

The files belonging to this database system will be owned by user "Construct".
This user must also own the server process.

The database cluster will be initialized with locale en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".

creating directory /usr/local/var/postgres ... initdb: could not create directory "/usr/local/var": Permission denied
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Jimmy Odom
  • 395
  • 1
  • 3
  • 5

5 Answers5

83

This should work just fine:

# sudo mkdir /usr/local/var/postgres
# sudo chmod 775 /usr/local/var/postgres
# sudo chown construct /usr/local/var/postgres
# initdb /usr/local/var/postgres

use your username in place of construct. So, if your computer username is WDurant, the code will be:

# sudo chown $(whoami) /usr/local/var/postgres
Gaurav Swaroop
  • 1,181
  • 11
  • 8
  • 7
    `sudo chown $(whoami) /usr/local/var/postgres/` – jhrr Jun 15 '16 at 13:47
  • 1
    Sorry to hear you've been having such problems @Geuis. I should have added some context to indicate that I was just trying to show that `sudo chown $(whoami) /usr/local/var/postgres/` is just a way to programmatically determine your username with `$(whoami)` instead of having to type it out as in `sudo chown wdurant /usr/local/var/postgres`. Hope you can get everything resolved, in any case. Good luck. – jhrr Jul 03 '16 at 14:09
  • 1
    Thanks @jhrr. Ugh sorry for the ugly comment last night. Wasn't really directed towards you personally. Just a lot of frustration on my end trying to get this setup. I deleted the prior comment but I'll leave this section here for anyone else that needs this in the future. "The issue isn't just the owner, its the permissions on the directory. No other answer has pointed out needing to adjust the directory perms. chmod 755 on the directory" – Geuis Jul 03 '16 at 22:23
  • No worries, we've all been there. – jhrr Jul 04 '16 at 11:37
  • Using homebrew, I followed the first three lines with `brew postinstall postgresql` and finally my issue seems to be resolved. – Adam Starrh Jul 10 '19 at 23:10
22

If you run on Arch Linux, use like this :

sudo mkdir /var/lib/postgres
sudo chmod 775 /var/lib/postgres
sudo chown postgres /var/lib/postgres

sudo -i -u postgres

[postgres]$ initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data'
[postgres]$ exit

sudo systemctl start postgresql.service

sudo -i -u postgres
halfer
  • 19,824
  • 17
  • 99
  • 186
Vibhutha Kumarage
  • 1,372
  • 13
  • 26
7

You actually need to SU to the postgres user

  • sudo su - postgres

then you can run the command

  • initdb -E UTF8 (I prefer setting this now because UTF8 is quite flexible and this will create all clusters as UTF8 [unless you explicitly specify otherwise])

then you need to create your user (if it hasn't already been created)

  • $ createuser -s -U postgres
  • $ Enter name of role to add: {{ my login name }} (this appears to be Construct)

then you can exit out of postgres user and you can create your own database

  • $ createdb
swasheck
  • 4,644
  • 2
  • 29
  • 56
3

Which user are you running initdb as? If you're not root, you likely don't have permission to create directories in /usr/local. I suggest creating /usr/local/var/postgres as root, and chown'ing it to construct:

# mkdir -m 0700 -p /usr/local/var/postgres
# chown construct /usr/local/var/postgres

Then your initdb (run as construct) should have permission.

Also, note that Unix usernames are normally all-lowercase (but also case-sensitive); are you sure your Construct user is actually capitalized? If so, are you really sure your want it to be capitalized—-a lot of things will break.

(FYI: For Unix questions like this, you may find Unix.SE or Ask Ubuntu provide quicker answers)

Community
  • 1
  • 1
derobert
  • 49,731
  • 15
  • 94
  • 124
0

Nowadays this should be, at least in CentOS/RHEL:

# install the binaries
sudo yum install -y postgresql-server
# create the database
sudo su postgres -c 'postgresql-setup initdb'
# enable the service to start on VM start
sudo systemctl enable postgresql.service
# start the service
sudo systemctl start postgresql.service

Then you can access it with

sudo -u postgresql psql
eis
  • 51,991
  • 13
  • 150
  • 199