简体   繁体   English

如何从 Ruby On Rails 应用程序连接到 Postgresql

[英]How to connect to Postgresql from Ruby On Rails App

I am new in Ruby on rails and i am using ruby v 1.9.3 & rails 3.2.12 with window 7我是 Ruby on rails 的新手,我正在使用 ruby​​ v 1.9.3 & rails 3.2.12 和 window 7

i want to PostgreSQL database connection in my app.我想在我的应用程序中连接 PostgreSQL 数据库。

this is my db connection这是我的数据库连接

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

this connection every time give error这个连接每次都报错

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)

Please help how to create PostgreSQL database connection in my RoR app.请帮助如何在我的 RoR 应用程序中创建 PostgreSQL 数据库连接。

First install pg gem首先安装 pg gem

    gem install pg

your apps database.yml file shoud be like this你的应用程序 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

Now run现在运行

rake db:setup    
rake db:migrate

and after this run在这次运行之后

rails s  

You can change host: 127.0.0.1 to host: localhost .您可以将host: 127.0.0.1更改为host: localhost It should solve the issue.它应该可以解决问题。

Use this link to verify, Is Postgresql installed successfully or not.使用此链接来验证 Postgresql 是否安装成功。

DigitalOceanCommunityArticle Click Here DigitalOceanCommunity文章点击这里

AskUbuntuLink Click Here AskUbuntuLink 点击这里

Removed host from development config in database.yml ,its worked for me.database.yml开发配置中删除了host ,它对我有用。

development:  
      adapter: postgresql
      database: tes
      pool: 5
      timeout: 5000
      username: postgres
      password: root

First you should install PostgreSQL and PgAdmin首先你应该安装 PostgreSQL 和 PgAdmin

After installing this thing you will have to add some pic of code ( to connect PostgreSQL to your rails project ) in your project config/database.yml file (before add PostgreSQL code you will have to remove all Sqlite3 code from config/database.yml file).安装这个东西后,你必须在你的项目 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 is default username and password in PostgreSQL database\\ postgres 是 PostgreSQL 数据库中的默认用户名和密码\\

after add this pic of code you have to add one line of code in your rails project root directory in Gemfile添加此代码图片后,您必须在 Gemfile 的 rails 项目根目录中添加一行代码

gem ‘pg’

and you must have to remove sqlite3 code from Gemfile ( gem 'sqlite3', '~> 1.4' )before adding gem 'pg'并且您必须在添加 gem 'pg' 之前从 Gemfile ( gem 'sqlite3', '~> 1.4' ) 中删除 sqlite3 代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM