[英]How to connect to Postgresql from Ruby On Rails App
我是 Ruby on rails 的新手,我正在使用 ruby v 1.9.3 & rails 3.2.12 和 window 7
我想在我的應用程序中連接 PostgreSQL 數據庫。
這是我的數據庫連接
development:
adapter: postgresql
encoding: unicode
database: mmagap_development
pool: 5
timeout: 5000
host: 127.0.0.1
username: postgres
password: root
test:
adapter: postgresql
encoding: unicode
database: mmagap_test
pool: 5
username: postgres
password: root
production:
adapter: postgresql
host: 127.0.0.1
encoding: unicode
database: mmagap_production
pool: 5
username: postgres
password: root
這個連接每次都報錯
D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in
`initialize': received invalid response to SSL negotiation: - (PG::ConnectionBad)
請幫助如何在我的 RoR 應用程序中創建 PostgreSQL 數據庫連接。
首先安裝 pg gem
gem install pg
你的應用程序 database.yml 文件應該是這樣的
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
現在運行
rake db:setup
rake db:migrate
在這次運行之后
rails s
您可以將host: 127.0.0.1
更改為host: localhost
。 它應該可以解決問題。
從database.yml
開發配置中刪除了host
,它對我有用。
development:
adapter: postgresql
database: tes
pool: 5
timeout: 5000
username: postgres
password: root
首先你應該安裝 PostgreSQL 和 PgAdmin
安裝這個東西后,你必須在你的項目 config/database.yml 文件中添加一些代碼圖片(將 PostgreSQL 連接到你的 rails 項目)(在添加 PostgreSQL 代碼之前,你必須從 config/database.yml 中刪除所有 Sqlite3 代碼文件)。
development:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
host: localhost
username: your_PostgreSQL_username
password: your_PostgreSQL_password
test:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
username: your_PostgreSQL_username
password: your_PostgreSQL_password
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
postgres 是 PostgreSQL 數據庫中的默認用戶名和密碼\\
添加此代碼圖片后,您必須在 Gemfile 的 rails 項目根目錄中添加一行代碼
gem ‘pg’
並且您必須在添加 gem 'pg' 之前從 Gemfile ( gem 'sqlite3', '~> 1.4' ) 中刪除 sqlite3 代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.