我正在关注最近设置PostgreSQL的RailsCast,但是我无法运行initdb /usr/local/var/postgres
命令。每次运行它,我都会收到此错误:
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
答案 0 :(得分:54)
这应该可以正常工作:
# 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
使用您的用户名代替构造。因此,如果您的计算机用户名是WDurant,则代码为:
# sudo chown wdurant /usr/local/var/postgres
答案 1 :(得分:7)
如果你在 arch linux 上运行,请使用如下:
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
答案 2 :(得分:5)
您确实需要SU
postgres
用户
sudo su - postgres
然后你可以运行命令
initdb -E UTF8
(我现在更喜欢设置它,因为UTF8非常灵活,这会将所有群集创建为UTF8 [除非您另行明确指定])然后您需要创建您的用户(如果尚未创建)
$ createuser -s -U postgres
$ Enter name of role to add: {{ my login name }}
(这似乎是Construct
)然后你可以退出postgres用户,你可以创建自己的数据库
$ createdb
答案 3 :(得分:3)
您在运行initdb的用户是哪个?如果您不是root用户,则可能无权在/ usr / local中创建目录。我建议以root身份创建/ usr / local / var / postgres,然后将它构建为:
# mkdir -m 0700 -p /usr/local/var/postgres
# chown construct /usr/local/var/postgres
然后你的initdb(作为构造运行)应该有权限。
另请注意,Unix用户名通常全小写(但也区分大小写);你确定你的Construct用户实际上是大写的吗?如果是这样,你是否真的确定你希望它被大写 - 很多事情都会破裂。
(仅供参考:对于像这样的Unix问题,您可能会发现Unix.SE或Ask Ubuntu提供更快的答案)
答案 4 :(得分:0)
现在至少在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
然后您可以使用
进行访问sudo -u postgresql psql