1

I'm converting my data base from sqlite3 to PostgreSQL in my rails project, so I follow this tutorial : https://devcenter.heroku.com/articles/sqlite3

I suceed to make all the needed changes but at the end when I want to make the command :

rake db:create 
rake db:migrate 

I have theses errors :

   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}

Here my code :

photo.rb :

class Photo < ActiveRecord::Base
    has_attached_file :image
end

Gemfile :

# 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 :

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 :

# 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

Do you have an idea to make my migration work ? Thank's

Rabbit56
  • 196
  • 3
  • 10

2 Answers2

1

You should follow this guide on setting up Postgres and Rails:

This section of the tutorial shows how to set up a user for 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):

create role myapp with createdb login password 'password1'

However, you should use ubuntu instead of myapp to create the necessary role.

Farley Knight
  • 1,795
  • 1
  • 13
  • 17
0

Your database doesn't have the role 'ubuntu.' Which is what you put as your user in your database.yml file. You'll have to create it. While in postgres run:

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

You can then alter the role permissions with ALTER ROLE. Such allowing user to create roles and databases with something like:

   ALTER ROLE owlphabet CREATEROLE CREATEDB REPLICATION SUPERUSER;

I found this link very useful.

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

Also you didn't include the role/user in your database.yml - You'll need to make them look something like this:

    development:
    adapter: postgresql
    encoding: unicode
    database: my_data_base_development
    pool: 5
    username: ubuntu
    password: type_your_password
    host: localhost

Change for production and test too of course.

nil
  • 2,238
  • 1
  • 19
  • 28