簡體   English   中英

Rails與應用程序ubuntu中的Postgresql數據庫連接

[英]Rails Connect with Postgresql database in application ubuntu

我正在使用ubuntu 12.04rails 3.2 我正在創建一個使用PostgreSQL數據庫的Rails應用程序。 我使用以下命令安裝了postgresql:

sudo apt-get install postgresql

作為參考,我簽出了https://help.ubuntu.com/community/PostgreSQL 后來我創建了用戶postgres並使用以下命令設置了密碼postgres

sudo -u postgres psql postgres
\password postgres

接下來,我使用以下方法創建數據庫:

 sudo -u postgres createdb mydb

我嘗試使用用戶名postgres和密碼postgres與Postgresql連接,並成功連接了以下命令:

psql -U postgres -h localhost
Password for user postgres:
psql (9.1.4)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
postgres=#

在我的Rails應用中,我的database.yml具有以下代碼:

development:
 adapter: postgresql
 encoding: unicode
 database: mydb_development
 pool: 5
 username: postgres
 password: postgres

test:
 adapter: postgresql
 encoding: unicode
 database: mydb_test
 pool: 5
 username: postgres
 password: postgres

production:
 adapter: postgresql
 encoding: unicode
 database: mydb_production
 pool: 5
 username: postgres
 password: postgres

現在,當我運行命令rake db:migrate ,出現以下錯誤:

rake aborted!
FATAL:  Peer authentication failed for user "postgres"

我嘗試為每種環境將host:localhost添加到database.yml中,並且出現以下錯誤:

rake aborted!
couldn't parse YAML at line 28 column 0

28行是

development:
 adapter: postgresql
 encoding: unicode
 database: hackathonio_development
 pool: 5
 username: postgres
 password: testing
 host: localhost {This is line 28}

請幫我找出解決方案。

我認為您可能有2個問題。 首先,沒有如Shreyas所指出的那樣設置主機。 但是我相信第二個問題是,當您設置主機名時,Rails試圖通過tcp套接字而不是本地ruby套接字連接到PostgreSQL。 我的猜測是您需要修改pg_hba.conf文件,以允許postgres通過本地主機登錄。 以下是一些可能會有所幫助的SO問題。

Rails無法登錄到PostgreSQL-PG ::錯誤-密碼-正確的信息

pg_hba.conf中的“本地”和“本地”連接類型有什么區別?

無法在新數據庫中將Postgres用戶用於ubuntu 10.04服務器上的Rails 3

請添加主機到您的yml作為本地主機

我的建議:

第1步

通過運行來創建新的其他用戶

$ createuser <username>

在命令行上。 系統將提示您輸入密碼等。(示例/文檔: http : //www.postgresql.org/docs/8.1/static/app-createuser.html

第2步

使用新用戶的用戶名/密碼更新database.yml 至少暫時不要忘記您創建的第一個用戶。

第2步

$ rake db:create

第三步

$ rake db:migrate

我認為這些步驟比您嘗試的更有可能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM