postgresql createdb:数据库创建失败:错误:创建数据库的权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43734650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
createdb: database creation failed: ERROR: permission denied to create database
提问by SonamGupta
I am pretty much confused about root user,super user,userand permissions! I am not able to create a database inside user "athleticu". Following are the commands I used:-
我对root 用户、超级用户、用户和权限非常困惑!我无法在用户“athleticu”中创建数据库。以下是我使用的命令:-
athleticu@ip-172-30-4-103:/home/ubuntu$ createdb -T template0 simple_db1
createdb: database creation failed: ERROR: permission denied to create database
athleticu@ip-172-30-4-103:/home/ubuntu$ sudo createdb -T template0 simple_db1
sudo: unable to resolve host ip-172-30-4-103
createdb: could not connect to database template1: FATAL: role "root" does not exist
Please somebody clarify my doubts and tell me what should I write!
请有人澄清我的疑问并告诉我我应该写什么!
回答by SonamGupta
Hey I have already solved this. What you have to do is to first login as postgres user as follows:
嘿我已经解决了这个问题。您需要做的是首先以 postgres 用户身份登录,如下所示:
$ su postgres
$ psql
postgres=# alter user athleticu createdb;
ALTER ROLE
Hope it helps you :)
希望对你有帮助:)
回答by polyccon
Type \du
in psql and you will see a list of all the registered users and what type of privileges each one has.
In order to grant privileges to the user which is logged in (eg 'user1'), I had to sign out and log in using one of the superuser roles in that list (eg. 'user2'), using the following command:
键入\du
在psql里,你会看到所有注册用户的列表和什么类型的权限,每个人都有。为了向登录的用户(例如“user1”)授予权限,我必须使用以下命令注销并使用该列表中的超级用户角色之一(例如“user2”)登录:
psql -U 'user2' -h localhost 'database2'
where 'database2' is the name of the one that specific superuser 'user2' has privileges to. Once you are logged in as a superuser, you can grant privileges to 'user1' by:
其中 'database2' 是特定超级用户 'user2' 拥有权限的用户名。以超级用户身份登录后,您可以通过以下方式向“user1”授予权限:
ALTER ROLE user1 WITH CREATEDB
or
或者
ALTER ROLE user1 WITH SUPERUSER
Then sign in again as user1, who is now a superuser.
然后以 user1 再次登录,他现在是超级用户。
回答by dmfay
The root useris an account on the system independent from Postgres. There is only one root user.
的根用户是系统独立于所述的Postgres一个帐户。只有一个 root 用户。
A superuseris an account in Postgres with access to everything. There may be many superusers.
一个超级用户是Postgres的访问到所有的账户。可能有很多超级用户。
System accounts and Postgres accounts are different things, although unless you specify a Postgres username when you connect to the database (through utilities like psql
, createdb
, dropdb
, or otherwise), it will use the current system user's name in hopes that there is a corresponding Postgres account with the same name. The root userdoes not, by default, have a corresponding account in Postgres.
系统帐户和Postgres账户是不同的东西,但除非你指定一个Postgres的用户名,当你连接到数据库(通过实用工具,比如psql
,createdb
,dropdb
,或以其他方式),将在希望使用当前系统用户的名称,有一个相应的Postgres帐户同名。该root用户不,默认情况下,在Postgres的一个相应的账户。
When you install Postgres on *nix, it creates both a superusernamed postgres
and a system user named postgres
.
当您在 *nix 上安装 Postgres 时,它会创建一个名为的超级用户postgres
和一个名为的系统用户postgres
。
Therefore, when you need to do something with Postgres as the built-in superuser, you have two options:
因此,当您需要使用 Postgres 作为内置超级用户执行某些操作时,您有两个选择:
- You may
sudo su - postgres
to become thepostgres
system userand execute your command (createdb
,psql
, etc). Because the system user has the same name as the database superuser, your command will connect as the appropriate account. - You may specify the username to execute as with the
-U
switch, egpsql -U postgres ...
.
- 您可能会
sudo su - postgres
成为postgres
系统用户和执行你的命令(createdb
,psql
,等)。由于系统用户与数据库超级用户同名,因此您的命令将以适当的帐户进行连接。 - 您可以指定要执行的用户名与
-U
开关一样,例如psql -U postgres ...
。
Depending on your Postgres server's authentication settings, you may be required to enter a password with either or both connection methods.
根据您的 Postgres 服务器的身份验证设置,您可能需要使用一种或两种连接方法输入密码。
回答by nyxz
What you can do when you have fresh installation of PostgreSQL is create your user with some rights (see createuser documentation):
当您全新安装 PostgreSQL 时,您可以做的是创建具有某些权限的用户(请参阅createuser 文档):
my-user> sudo su - postgres -c "createuser <my-user> --createdb"
This will allow my-user
to create DBs just like so:
这将允许my-user
像这样创建数据库:
my-user> createdb <my-db>
If you want the my-user
to be able to do anything just use the --superuser
flag instead:
如果您希望my-user
能够执行任何操作,请改用该--superuser
标志:
my-user> sudo su - postgres -c "createuser <my-user> --superuser"