Ruby on rails 4 如何让rails应用程序使用postgresql数据库?

Ruby on rails 4 如何让rails应用程序使用postgresql数据库?,ruby-on-rails-4,postgresql-9.2,Ruby On Rails 4,Postgresql 9.2,我安装了Prosgres与我的rails4应用程序一起使用,我能够连接到数据库服务器并创建数据库。然后,我对Gemfile和config/database.yml进行了必要的更改,以使用postgres。然后我创建了一个新应用程序,并在该应用程序中创建了一个用户模型: $ rails generate model User name:string email:string invoke active_record create db/migrate/2013080

我安装了Prosgres与我的rails4应用程序一起使用,我能够连接到数据库服务器并创建数据库。然后,我对Gemfile和config/database.yml进行了必要的更改,以使用postgres。然后我创建了一个新应用程序,并在该应用程序中创建了一个用户模型:

$ rails generate model User name:string email:string
      invoke  active_record
      create    db/migrate/20130806145935_create_users.rb
      create    app/models/user.rb
      invoke    rspec
      create      spec/models/user_spec.rb
然后我做了:

$ bundle exec rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.1498s
==  CreateUsers: migrated (0.1500s) ===========================================
但在db目录中,我看到以下文件:

development.sqlite3
test.sqlite3
此外,当我尝试使用SQLite数据库浏览器查看这些文件时,我会得到一个错误,表明它们不是SQLite 3数据库。事实上,它们是空的:

~/rails_projects/sample_app4_0/db$ ls -al
total 40
drwxr-xr-x   8 7stud  staff    272 Aug  6 22:50 .
drwxr-xr-x  24 7stud  staff    816 Aug  6 22:23 ..
-rw-r--r--   1 7stud  staff      0 Jul 30 00:21 development.sqlite3
drwxr-xr-x   3 7stud  staff    102 Aug  6 22:22 migrate
-rw-r--r--   1 7stud  staff   1063 Aug  6 09:06 schema.rb
-rw-r--r--   1 7stud  staff    343 Jul 29 20:01 seeds.rb
-rw-r--r--   1 7stud  staff      0 Jul 30 03:02 test.sqlite3
我听说你可以用一个标志来创建rails应用程序,告诉它使用postgres:

rails new myapp --database postgresql
但我已经创建了我的应用程序。我必须重新开始吗?如果这有区别的话,我会使用git

我的档案:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
#Added for postgres:
gem 'pg', '0.15.1'


group :development, :test do
  #gem 'sqlite3', '1.3.7'
  gem 'rspec-rails', '2.13.1'
  gem 'guard-rspec', '2.5.0'

  gem 'spork-rails', github: 'sporkrb/spork-rails'
  gem 'guard-spork', '1.5.0'
  gem 'childprocess', '0.3.6'
end

group :test do
  gem 'selenium-webdriver', '2.0.0'
  gem 'capybara', '2.1.0'
  gem 'growl', '1.0.3'
end

gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  #gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end
config/database.yml:

development:
  adapter: postgresql
  encoding: utf8
  database: sampleapp_dev  #can be anything unique

  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.

test:
  adapter: postgresql
  encoding: utf8
  database: sampleapp_test  #can be anything unique

  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: sampleapp_prod 

  pool: 5
  timeout: 5000
development:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_development
  pool: 5

test:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_test
  pool: 5

production:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_production
  pool: 5

好的,为了弄清楚发生了什么,我使用-database标志创建了一个新的测试应用程序:

rails_projects$ rails new test_postgres --database postgresql
事实证明,所做的只是像这样设置config/database.yml文件:

development:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_development
  pool: 5
  username: test_postgres
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_test
  pool: 5
  username: test_postgres
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_production
  pool: 5
  username: test_postgres
  password:
以及空的sqlite文件:

development.sqlite3
test.sqlite3 
数据库目录中仍然存在

然后我启动了postgres,并使用pgAdmin3连接到postgres数据库服务器。如果这令人困惑,请参阅:

然后我创建了一个模型:

~/rails_projects$ cd test_postgres/
~/rails_projects/test_postgres$ rails g scaffold Post title:string author:string body:text

      invoke  active_record
      create    db/migrate/20130807061320_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/models/post_test.rb
      create      test/fixtures/posts.yml
      invoke  resource_route
       route    resources :posts
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb
      invoke    erb
      create      app/views/posts
      create      app/views/posts/index.html.erb
      create      app/views/posts/edit.html.erb
      create      app/views/posts/show.html.erb
      create      app/views/posts/new.html.erb
      create      app/views/posts/_form.html.erb
      invoke    test_unit
      create      test/controllers/posts_controller_test.rb
      invoke    helper
      create      app/helpers/posts_helper.rb
      invoke      test_unit
      create        test/helpers/posts_helper_test.rb
      invoke    jbuilder
      create      app/views/posts/index.json.jbuilder
      create      app/views/posts/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/posts.js.coffee
      invoke    scss
      create      app/assets/stylesheets/posts.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss
然后我尝试执行迁移:

~/rails_projects/test_postgres$ bundle exec rake db:migrate
rake aborted!
FATAL:  role "test_postgres" does not exist
...
...
发生这个错误是因为我在postgres中没有一个名为test_postgres的数据库用户或角色,其他人也可能不会。当我设置postgres时,我创建了用户7stud,这是我的Mac用户名。为了修复rails错误,我在config/database.yml的三行中将用户名改为7stud。编辑:事实上,我甚至不需要这样做。因为我是Mac上的用户7stud,这意味着我正在以7stud的身份执行rails命令,再加上我的postgres db是使用名为7stud的用户设置的,这允许我完全消除config/database.yml中的用户名行:

development:
  adapter: postgresql
  encoding: utf8
  database: sampleapp_dev  #can be anything unique

  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.

test:
  adapter: postgresql
  encoding: utf8
  database: sampleapp_test  #can be anything unique

  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: sampleapp_prod 

  pool: 5
  timeout: 5000
development:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_development
  pool: 5

test:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_test
  pool: 5

production:
  adapter: postgresql
  encoding: unicode
  database: test_postgres_production
  pool: 5
然后我再次尝试执行迁移:

~/rails_projects/test_postgres$ bundle exec rake db:migrate
rake aborted!
FATAL:  database "test_postgres_development" does not exist
...
...
~/rails_projects/test_postgres$ bundle exec rake db:migrate
==  CreatePosts: migrating ====================================================
-- create_table(:posts)
   -> 0.0441s
==  CreatePosts: migrated (0.0443s) ===========================================
请注意,database.yml文件指定了三个数据库名称。因此,我创建了开发和测试数据库:

~$ cd /Library/PostgreSQL/9.2/
/Library/PostgreSQL/9.2$ sudo su postgres
Password: <normal sudo password>
bash-3.2$ createdb -O7stud -Eutf8 test_postgres_development
bash-3.2$ createdb -O7stud -Eutf8 test_postgres_test
成功,但在网上其他地方的例子显示不同的输出与'通知'行

然后我启动了一个服务器:

~/rails_projects/test_postgres$ rails s
…我在浏览器中输入了url:

http://localhost:3000/posts
…我还写了几个帖子

然后使用pgAdmin3,我尝试在test_postgres_开发数据库中查找帖子。首先,我通过右键单击行并选择“刷新”,刷新了pgAdmin3中的“Databases3”行。然后我在test\u postgres\u开发目录中搜索了很长一段时间,没有找到任何运气。在Schemas下,我可以看到一个包含posts目录的Tables目录,因此创建了该表,但我不知道如何查看该表中是否有任何条目。事实证明,你必须右键点击posts目录,然后选择viewdata,瞧,这就是我创建的posts

或者,您可以使用命令行查看条目:

~$ cd /Library/PostgreSQL/9.2/
/Library/PostgreSQL/9.2$ sudo su postgres
Password: <normal sudo password>
bash-3.2$ psql -U 7stud test_postgres_development
psql (9.2.4)
Type "help" for help.

test_postgres_development=> \dt

             List of relations
 Schema |       Name        | Type  | Owner 
--------+-------------------+-------+-------
 public | posts             | table | 7stud
 public | schema_migrations | table | 7stud
(2 rows)

test_postgres_development=> \d posts

                                     Table "public.posts"
   Column   |            Type             |                     Modifiers                      
------------+-----------------------------+----------------------------------------------------
 id         | integer                     | not null default nextval('posts_id_seq'::regclass)
 title      | character varying(255)      | 
 author     | character varying(255)      | 
 body       | text                        | 
 created_at | timestamp without time zone | 
 updated_at | timestamp without time zone | 
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)


test_postgres_development=> select id, title, author, created_at from posts;


 id |         title          |    author    |         created_at         
----+------------------------+--------------+----------------------------
  1 | Hi there.              | Tom          | 2013-08-07 06:24:57.806237
  2 | Ola!                   | Nancy        | 2013-08-07 06:25:23.989643
(2 rows)

test_postgres_development=>