Trying to get Postgres setup in my environment but can't seem to get permissions to intidb

43,764

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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)

Share:
43,764
Jimmy Odom
Author by

Jimmy Odom

Updated on December 16, 2020

Comments

  • Jimmy Odom
    Jimmy Odom over 3 years

    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