質問

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
役に立ちましたか?

解決

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

他のヒント

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

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

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)

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top