2011-12-26 48 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 

應該努力!

相關問題