Ruby on rails:数据库sqlite3到PostgreSQL [RAILS]

Ruby on rails : Data base sqlite3 to PostgreSQL [RALS]

本问题已经有最佳答案,请猛点这里访问。

我正在我的rails项目中将我的数据库从sqlite3转换为PostgreSQL,所以我遵循这个教程:https://devcenter.heroku.com/articles/sqlite3

我完成了所有必要的更改,但最后我想要发出命令:

1
2
rake db:create
rake db:migrate

我有这些错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
   FATAL:  role"ubuntu" does not exist
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `initialize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `
new'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `
new'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `
checkout_new_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `
mon_synchronize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `
mon_synchronize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `
retrieve_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `
create'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:72:in `create'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:86:in `block in create_current'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:182:in `
block in each_current_configuration'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each_current_configuration'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:85:in `
create_current'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:20:in `block (2 levels) in <top (required)>'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:236:in `
block in execute'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:175:in `
block in invoke_with_call_chain'
    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:161:in `
invoke'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `
each'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:100:in `
top_level'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:75:in `
run'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `load'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `
<main>'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
    Couldn't create database for {"adapter"=>"postgresql","database"=>"my_data_base_development","pool"=>5,"timeout"=>5000}
    FATAL:  role"ubuntu" does not exist
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `
initialize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `new'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `
initialize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `
new_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `
block in checkout'
    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `
block in connection'
    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `
retrieve_connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `
connection'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:72:in `create'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:86:in `
block in create_current'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:182:in `block in each_current_configuration'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `
each_current_configuration'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:85:in `create_current'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:20:in `block (2 levels) in <top (required)>'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:236:in `
call'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:231:in `
execute'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'

    /home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:168:in `
invoke_with_call_chain'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `
block (2 levels) in top_level'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:115:in `
run_with_threads'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:165:in `
standard_exception_handling'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `
load'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `<main>'

    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
    /home/ubuntu/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `
<main>'
    Couldn'
t create database for {"adapter"=>"postgresql","database"=>"my_data_base_test","pool"=>5,"timeout"=>5000}

这是我的代码:

photo.rb:

1
2
3
class Photo < ActiveRecord::Base
    has_attached_file :image
end

Gemfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2'
gem"paperclip","~> 3.0"
gem"rake"

# Use sqlite3 as the database for Active Record
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

XXXXXXXXXXXX_create_image.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CreatePhotos < ActiveRecord::Migration
  def change
    create_table :photos do |t|

      t.timestamps
    end
  end
   def self.up
  add_column :photos, :image_file_name, :string
  add_column :photos, :image_content_type, :string
  add_column :photos, :image_file_size, :string
  add_column :photos, :image_update_at, :string
  end

  def self.down
  remove_column :photos, :image_file_name, :string
  remove_column :photos, :image_content_type, :string
  remove_column :photos, :image_file_size, :string
  remove_column :photos, :image_update_at, :string
  end
end

database.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: postgresql
  database: my_data_base_development
  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
  database: my_data_base_test
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: my_data_base_production
  pool: 5
  timeout: 5000

你有想法让我的迁移工作吗? 谢谢


您应该按照本指南设置Postgres和Rails:

本教程的这一部分显示了如何为Postgres设置用户:

Setting Up Postgres
Create a Postgres user for the Rails app we'll
create in the next step. To do this, switch into the Postgres user:

su - postgres Then create a user (or a"role", as Postgres calls it):

1
create role myapp with createdb login password 'password1'

但是,您应该使用ubuntu而不是myapp来创建必要的角色。


您的数据库没有"ubuntu"角色。 这是您在database.yml文件中作为用户使用的内容。 你必须创建它。 在postgres运行时:

1
  CREATE ROLE ubuntu WITH LOGIN ENCRYPTED PASSWORD 'type-your-password' CREATEDB;

然后,您可以使用ALTER ROLE更改角色权限。 这样允许用户使用以下内容创建角色和数据库:

1
   ALTER ROLE owlphabet CREATEROLE CREATEDB REPLICATION SUPERUSER;

我发现这个链接非常有用。

1
 http://blog.jasonmeridth.com/2012/10/02/postgresql-command-line-cheat-sheet.html

此外,您没有在database.yml中包含角色/用户 - 您需要使它们看起来像这样:

1
2
3
4
5
6
7
8
    development:
    adapter: postgresql
    encoding: unicode
    database: my_data_base_development
    pool: 5
    username: ubuntu
    password: type_your_password
    host: localhost

当然也要改变生产和测试。