我目前正在编写一个脚本来构建我的流浪盒。我安装了PHP 7和其他一些工具,以及PostgreSQL,但在创建数据库并尝试授予权限时,我收到以下错误
ERROR: relation "projectname" does not exist
这是我的剧本(重要的东西)
#!/usr/bin/env bash
projectname='projectname'
echo "Initializing database"
sudo apt-get install -y postgresql
sudo -u postgres psql -c "CREATE USER $projectname WITH PASSWORD 'xxxx';"
sudo -u postgres psql -c "CREATE DATABASE $projectname;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON $projectname TO $projectname;"
一切正常,直到最后一步
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON $projectname TO $projectname;"
尝试此操作时,我收到了上述错误。我也尝试手动编写
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON projectname TO projectname;"
同样的错误。
我也尝试用引号将其包装
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON 'feedparser' TO 'feedparser';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON \"feedparser\" TO \"feedparser\";"
我到底错在了什么?
答案 0 :(得分:5)
有关GRANT PRIVILEGES的文档说明您的GRANT版本命令适用于表的授予权限。如果要授予数据库权限,则应使用GRANT ALL PRIVILEGES ON DATABASE projectname TO projectname;
。下面我展示了这些命令的结果:
postgres=# CREATE USER projectname WITH PASSWORD 'projectname';
CREATE ROLE
postgres=# CREATE DATABASE projectname;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON projectname TO projectname;
ERROR: relation "projectname" does not exist
postgres=# GRANT ALL PRIVILEGES ON DATABASE projectname TO projectname;
GRANT