postgresql Postgres 用户不存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21122598/
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
Postgres user does not exist?
提问by krystah
I have just installed Postgres and have been tinkering with it and various configurations for 1-2 hours.
我刚刚安装了 Postgres,并且已经对它和各种配置进行了 1-2 个小时的修补。
I am stuck on being unable to change to the postgres-user
我一直无法更改为 postgres 用户
$ su - postgres
yields the following error: su: unknown login: postgres
$ su - postgres
产生以下错误: su: unknown login: postgres
$ sudo -u postgres psql
yields the following error: sudo: unknown user: postgres
$ sudo -u postgres psql
产生以下错误: sudo: unknown user: postgres
These attempts are made as the normal user. Trying them as root has the exact same results. I have installed postgres via Homebrew on OS X, and I have read the instructions multple times.
这些尝试是作为普通用户进行的。以 root 身份尝试它们具有完全相同的结果。我已经在 OS X 上通过 Homebrew 安装了 postgres,并且我已经多次阅读说明。
采纳答案by danielM
psql
: Logs me in with my default username
psql
: 使用我的默认用户名登录
psql -U postgres
: Logs me in as the postgres user
psql -U postgres
: 以 postgres 用户身份登录我
Sudo doesn't seem to be required for me.
我似乎不需要 Sudo。
I use Postgres.app for my OS X postgres database. It removed the headache of making sure the installation was working and the database server was launched properly. Check it out here: http://postgresapp.com
我将 Postgres.app 用于我的 OS X postgres 数据库。它消除了确保安装正常运行和数据库服务器正常启动的麻烦。在这里查看:http: //postgresapp.com
Edit: Credit to @Erwin Brandstetter for correcting my use of the arguments.
编辑:感谢@Erwin Brandstetter 纠正我对参数的使用。
回答by kangkyu
By psql --help
, when you didn't set options for database name (without -d
option) it would be your username, if you didn't do -U
, the database username would be your username too, etc.
By psql --help
,当您没有为数据库名称设置选项(没有-d
选项)时,它将是您的用户名,如果您没有这样做-U
,则数据库用户名也将是您的用户名,等等。
But by initdb
(to create the first database) command it doesn't have your username as any database name. It has a database named postgres
. The first database is always created by the initdb command when the data storage area is initialized. This database is called postgres.
但是通过initdb
(创建第一个数据库)命令,它没有您的用户名作为任何数据库名称。它有一个名为postgres
. 第一个数据库总是在数据存储区初始化时由 initdb 命令创建的。这个数据库叫做 postgres。
So if you don't have another database named your username, you need to do psql -d postgres
for psql command to work. And it seems it gives -d
option by default, psql postgres
also works.
所以如果你没有另一个名为你的用户名的数据库,你需要做psql -d postgres
psql 命令才能工作。似乎它-d
默认提供选项,psql postgres
也有效。
If you have created another database names the same to your username, (it should be done with createdb
) then you may command psql
only. And it seems the first database user name sets as your machine username by brew.
如果您创建了另一个与您的用户名相同的数据库(应该使用createdb
),那么您只能命令psql
。似乎第一个数据库用户名被 brew 设置为您的机器用户名。
psql -d <first database name> -U <first database user name>
or,
或者,
psql -d postgres -U <your machine username>
psql -d postgres
would work by default.
默认情况下会工作。
回答by Gordon Davisson
OS X tends to prefix the system account names with "_"; you don't say what version of OS X you're using, but at least in 10.8 and 10.9 the _postgres user exists in a default install. Note that you won't be able to su
to this account (except as root), since it doesn't have a password. sudo -u _postgres
, on the other hand, should work fine.
OS X 倾向于在系统帐户名前加上“_”;您没有说明您使用的是哪个版本的 OS X,但至少在 10.8 和 10.9 中,_postgres 用户存在于默认安装中。请注意,您将无法su
访问此帐户(root 用户除外),因为它没有密码。sudo -u _postgres
,另一方面,应该可以正常工作。
回答by daroo
the discussion and answer here was massively helpful to me:
这里的讨论和回答对我非常有帮助:
回答by cloudberry
psql -U postgres
Worked fine for me in case of db name: postgres & username: postgres. So you do not need to write sudo.
在数据库名称的情况下对我工作正常:postgres 和用户名:postgres。所以你不需要写 sudo。
And in the case other db, you may try
在其他数据库的情况下,您可以尝试
psql -U yourdb postgres
As it is given in Postgres help:
正如 Postgres 帮助中给出的那样:
psql [OPTION]... [DBNAME [USERNAME]]
回答by Léo Léopold Hertz ??
I get exactly the same errors as kryshah with su - postgres
and sudo -u postgres psql
.
DanielM's answer gives also errors.
我得到与 kryshah 完全相同的错误su - postgres
和sudo -u postgres psql
。DanielM 的回答也给出了错误。
Outputs when wrong settings
错误设置时的输出
Answer however from przbabu's comment.
然而,从 przbabu 的评论中回答。
masi$ psql
psql: FATAL: database "masi" does not exist
masi$ psql -U postgres
psql: FATAL: role "postgres" does not exist
masi$ psql postgres
psql (9.4.1)
Type "help" for help.
I think the some part of this problem may be in owner settings in OSX
我认为这个问题的一部分可能是在 OSX 的所有者设置中
masi$ ls -al /Users/
total 0
drwxr-xr-x 7 root admin 238 Jul 3 09:50 .
drwxr-xr-x 37 root wheel 1326 Jul 2 19:02 ..
-rw-r--r-- 1 root wheel 0 Sep 10 2014 .localized
drwxrwxrwt 7 root wheel 238 Apr 9 19:49 Shared
drwxr-xr-x 2 root admin 68 Jul 3 09:50 postgres
drwxr-xr-x+ 71 masi staff 2414 Jul 3 09:50 masi
but doing sudo chown -R postgres:staff /Users/postgres
gives chown: invalid user: ‘postgres:staff'
.
但做sudo chown -R postgres:staff /Users/postgres
给chown: invalid user: ‘postgres:staff'
。
In short, this is not the solution the problem. Use the tools provided by the postgres installation to create a user and database.
总之,这不是解决问题的方法。使用 postgres 安装提供的工具来创建用户和数据库。
To get right settings and outputs
获得正确的设置和输出
There are specific commands after postgres installation to add a new user to the database system. After initdb, run the following as described here
postgres 安装后有特定的命令可以将新用户添加到数据库系统。在 initdb 之后,按照此处所述运行以下命令
createuser --pwprompt postgres
createdb -Opostgres -Eutf8 masi_development
psql -U postgres -W masi_development
To avoid the password request all the time, you have three choices as described here.
为避免一直请求密码,您可以选择此处所述的三种选择。
回答by Adnan Fayaz
For me This was the solution on macOS ReInstall the psql
对我来说这是 macOS 上的解决方案重新安装 psql
brew install postgres
Start PostgreSQL server
启动 PostgreSQL 服务器
pg_ctl -D /usr/local/var/postgres start
Initialize DB
初始化数据库
initdb /usr/local/var/postgres
If this command throws an error the rm the old database file and re-run the above command
如果此命令抛出错误,则 rm 旧数据库文件并重新运行上述命令
rm -r /usr/local/var/postgres
Create a new database
创建一个新的数据库
createdb postgres_test
psql -W postegres_test
You will be logged into this db and can create a user in here to login
您将登录到此数据库并可以在此处创建用户进行登录
回答by el fuser
The solution is simple:
log in as root
and after:
解决方案很简单:
以 root 身份登录
,然后:
su - postgres
psql