【问题标题】:Role does not exist and unable to create database when using PostgreSQL使用PostgreSQL时角色不存在无法创建数据库
【发布时间】:2012-01-28 04:37:50
【问题描述】:

我在我的应用程序中使用 Heroku,它需要 PostgreSQL,但您仍然可以使用 SQLite3 进行开发。由于 Heroku 强烈建议不要使用 2 个不同的数据库,我决定改用 PostgreSQL 进行开发。我安装了gem pg,还访问了PostgreSQL 官方站点以获取Windows 安装程序,还更改了我的database.yml。在安装过程中,它需要 PostgreSQL 的密码,所以我做了一个。 在尝试创建数据库时,我必须将 pg_hba.conf 文件从使用 md5 更改为 trust 才能通过:fe_sendauth: no password supplied

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust # was md5
# IPv6 local connections:
host    all             all             ::1/128                 trust # was md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

在摆脱了它之后,我现在得到了这个:

$ rake db:create
(in C:/app)
FATAL:  role "User" does not exist 
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", 
"database"=>"app_test", "pool"=>5, "username"=>nil, "password"=>nil} 

我的development.sqlite3text.sqlite3 仍然存在,这可能是问题吗?必须做什么?

这是我的全部要点:https://gist.github.com/1522188

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 postgresql


    【解决方案1】:

    将用户名添加到您的database.yml,不妨使用您的应用程序名称(或名称的某些变体)作为用户名,我将使用app_name 作为占位符:

    development:
      adapter: postgresql
      encoding: utf8
      database: app_development
      pool: 5
      username: app_name
      password:
    

    然后使用 psql.exe 在 PostgreSQL 中创建用户(也称为“角色”):

    $ psql -d postgres
    postgres=# create role app_name login createdb;
    postgres=# \q
    

    第一行在您的终端中,接下来的两行在psql 中。然后做你的rake db:create

    User 用户可能是默认用户,但 user is already taken 在 PostgreSQL 中用于其他用途,因此如果您想使用 User 作为用户名,则必须引用它以保留大小写:

    postgres=# create role "User" login createdb;
    

    无论如何,您最好为每个应用程序创建一个用户。

    您也需要对 database.yml 中的 test 条目执行类似的操作。

    【讨论】:

    • 它不允许我进入psql.exe。当我尝试打开它时,窗口出现然后自动关闭,所以我尝试转到 cmd 提示符并输入c:\Program Files..\..\bin > psql.exe > psql: FATAL: role "User" does not exist 再次获得FATAL。有什么想法吗?
    • @wrbg:您在安装过程中创建了一个postgres 用户,对吧?试试psql -d postgres -U postgres,如果你需要密码,那么psql -d postgres -U postgres -W
    • @wrbg:你能psql -d postgres -U postgres吗?
    • @wrbg:尝试create role app_name login createdbalter role app_name createdb 然后rake db:create。我上次这样做时可能设置了不同的默认值。
    • @wrbg:您可以使用psql 中的\du 查看用户/角色及其权限,或使用\l 列出数据库。如果您的用户设置正确,那么rake db:create 应该创建数据库并且psql -U app_name -d app_development 应该进入它。
    【解决方案2】:

    如果您的config/database.yml 中未指定username,PostgreSQL 将尝试使用您的帐户(登录名)名称创建数据库。在 OS X 和 Linux 上,您可以通过whoami 看到这是谁。看起来您使用的是 Windows。

    解决方案 A: Create a PostgreSQL user 匹配它正在寻找的那个。例如

    createuser --superuser some_user
    

    解决方案 B: 通过显式设置用户名as shown in mu's answer来更改数据库用户。

    【讨论】:

    • 真的需要--superuser 还是-d 够用?
    【解决方案3】:

    如果您的机器上有一个特定的帐户/用户用于 postgres,例如称为 postgres。

    然后执行此命令将提示您输入角色名称。

    sudo -u postgres createuser --interactive
    

    然后做

    rake db:create
    

    应该工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2016-04-05
      • 1970-01-01
      • 2013-03-13
      相关资源
      最近更新 更多