2011-12-26 214 views
56

我为我的应用程序使用Heroku,它需要PostgreSQL,但仍可以使用SQLite3进行开发。由于Heroku强烈建议不要有2个不同的数据库,我决定改用PostgreSQL进行开发。我安装了gem pg,并且还去了官方的PostgreSQL站点以获得Windows安装程序,并且还更改了我的database.yml。在安装期间,它需要PostgreSQL的密码,所以我做了一个。 我不得不将pg_hba.conf文件从使用md5改为trust,以便在尝试创建数据库时获得过去:fe_sendauth: no password supplied使用PostgreSQL时,角色不存在并且无法创建数据库

# 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

回答

118

添加用户名到您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条目做类似的事情。

+1

它不让我进入'psql.exe'。当我尝试打开它时,窗口出现,然后自动关闭,所以我然后尝试进入cmd提示符并在'c:\ Program Files .. \ .. \ bin> psql.exe> psql:FATAL:角色“用户”不存在“再次获得”致命“。有任何想法吗? – LearningRoR 2011-12-26 23:40:37

+1

@wrbg:你在安装过程中创建了一个'postgres'用户,对吧?试试'psql -d postgres -U postgres',如果你需要密码,那么'psql -d postgres -U postgres -W'。 – 2011-12-26 23:49:14

+0

它在安装期间没有提示用户。让我看看重新安装。 – LearningRoR 2011-12-27 00:12:56

6

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

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

createuser --superuser some_user 

溶液B: 更改DB用户通过显式设置用户名as shown in mu's answer

+0

Youdaman!这是唯一为我工作的人。 – 2016-04-11 22:01:34

+0

它真的需要'--superuser'还是'-d'足够了? – Meekohi 2017-09-15 20:59:03

4

如果您的计算机上有特定的帐户/用户,例如postgres,称为postgres。

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

sudo -u postgres createuser --interactive 

然后做

rake db:create 

应该努力!

相关问题