我正在按照最近的 RailsCast 设置 PostgreSQL,但我无法运行
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
这应该可以正常工作:
# sudo mkdir /usr/local/var/postgres
# sudo chmod 775 /usr/local/var/postgres
# sudo chown $(whoami) /usr/local/var/postgres/
# initdb /usr/local/var/postgres
使用您的用户名代替构造。因此,如果您的计算机用户名是 WDurant,则代码将为:
# sudo chown $(whoami) /usr/local/var/postgres
如果你在 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
您实际上需要
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
您以哪个用户身份运行 initdb?如果您不是 root,您可能无权在 /usr/local 中创建目录。我建议以 root 身份创建 /usr/local/var/postgres,然后 chown 来构建:
# mkdir -m 0700 -p /usr/local/var/postgres
# chown construct /usr/local/var/postgres
那么你的 initdb (作为构造运行)应该有权限。
另请注意,Unix 用户名通常全部小写(但也区分大小写);您确定您的 Construct 用户实际上是大写的吗?如果是这样,您是否真的确定想要将其大写——很多事情都会被破坏。 (仅供参考:对于此类 Unix 问题,您可能会发现
Unix.SE或 Ask Ubuntu 提供更快的答案)
# 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