rake assets:precompile RAILS_ENV=production Error - ruby-on-rails

I want to deploy a spree app to heroku and in order to do that I need to precompile my assets locally
I did
heroku addons:create heroku-postgresql
then I added config/application.rb
config.assets.initialize_on_precompile = false
my database.yaml file is
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
host: localhost
database: anzels_development
username: anzels
password: 1234
test:
<<: *default
host: localhost
database: anzels_test
username: anzels
password: 1234
production:
adapter: postgresql
encoding: unicode
database: anzels_production
pool: 5
password:
and whenever I run
sumeet#sumi-pc:~/anzels$ rake assets:precompile RAILS_ENV=production
I get an error
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "sumeet" does not exist
/home/sumeet/anzels/config/environment.rb:5:in `<top (required)>'
PG::ConnectionBad: FATAL: role "sumeet" does not exist
/home/sumeet/anzels/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
config/enviormenr.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
please help

In local computer you are trying to run
rake assets:precompile RAILS_ENV=production
but this requires your local compute to have config/database.yml with the config mentioned by #thieu-nguyen
so add the following in
username: $PRODUCTION_DB_USER
password: $PRODUCTION_DB_PASS
under production in config/database.yml
then add the environment for you local computer as
PRODUCTION_DB_USER=anzels
PRODUCTION_DB_PASS=1234
and for heroku as
PRODUCTION_DB_USER=user
PRODUCTION_DB_PASS="" (empty)
ANOTHER EASIER WAY IS
username: anzels
password: 1234
to production in config/database.yml **JUST BEFORE assests precompilation **
then run command
git checkout config/database.yml
JUST BEFORE GIT COMMIT command
the idea is to not to commit the username and password but temporarily edit it for assests precompilation purpose only

Please add username and password to production config in database.yml or you need to create new role for postgres with name "sumeet".
production:
adapter: postgresql
encoding: unicode
database: anzels_production
pool: 5
username: anzels
password: 1234

Related

Can't create production database for Rails app

I'm trying to create the database for my Rails app in production mode using Postgresql.
When I run the command:
$ RAILS_ENV=production rake db:create
I get the following error:
FATAL: database "postgres" does not exist
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "host"=>"localhost", "database"=>"provenword", "pool"=>5, "username"=>"postgres", "password"=>"password"}
I can create a database for older Rails apps that I have. However, only this one gives me this error. How can I get this to work so I can create the database and run the application?
Gem file:
..
gem 'pg'
..
Rails Version - 4.2.6
database YML
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
# default: &default
# user: postgres
# password: password
# pool: 5
# timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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:
<<: *default
database: db/test.sqlite3
production:
adapter: postgresql
encoding: unicode
host: localhost
database: provenword
pool: 5
username: postgres
password: password
Postgresql is installed on my server.
Thanks to #Dharam.
It's work for me as well:
I beleive postgresql expects a database with the same name as the user
name, in your case postgres, be present. First setup a database with
the name postgres then you can run RAILS_ENV=production rake db:create

rails is not using the configuration set on database.yaml

This is my database.yml
production:
adapter: mysql2
encoding: utf8
database: testing
pool: 5
username: ubuntu
host: 127.0.0.1
When I run rake db:setup I got this:
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"testing", "pool"=>5, "username"=>"ubuntu", "host"=>"127.0.0.1"}
-- create_table("documents", {:force=>:cascade})
rake aborted!
Mysql2::Error: Access denied for user ''#'localhost' to database 'testing'
Why it's not using my username and host? instead is showing '' # 'localhost'
I think, database.yml config for production environment, but you run it on development environment. You can use 'RAILS_ENV=development rake db:setup' or 'RAILS_ENV=test rake db:setup'
One more thing, database should be created by mysql user with root privileges

ActiveRecord::AdapterNotSpecified: 'postgresql' database is not configured

I trying to figure out how fix the active record problem so I can push to heroku.
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
ActiveRecord::AdapterNotSpecified: 'postgresql' database is not configured. Available: ["development", "adapter", "encoding",
"database", "host", "pool", "username", "password", "production"]
development:
adapter: postgresql
encoding: unicode
database: sample_app_development
host: localhost
pool: 5
username: sample_app
password:
test:
adapter: postgresql
encoding: unicode
database: sample_app_test
host: localhost
pool: 5
username: sample_app
password:
production:
adapter: postgresql
encoding: unicode
database: sample_app_production
host: localhost
pool: 5
username: sample_app
password:
There is a space before development in the db config. I believe To fix it, simply remove space from the first line and follow the indentation.
I think you need to write this command on terminal to create you database:
rake db:create
Then run rake db:migrate to run your migrations
Remove spaces on 1st line " development:"
Make sure of the database name e.g. "sample_app_development" or just "sample_app"
gem install pg
rake db:create
rake db:migrate
First install postgresql
gem install pg
Then create project using following command:
rails new myapp -d postgresql

rake aborted! can't convert nil into Hash

rake aborted! Cannot load Rails.application.database_configuration: can't convert nil into Hash
I am trying to deploy ruby on rails on openshift, and then i try to run rake db:migrate. Then i face this problem.
I create the app with https://github.com/kohjx/TestYourCode.git on openshift with ruby on rails + mysql 5.1
Then i configure the config/database.yml
default: &default
#adapter: sqlite3
adapter: mysql2
pool: 5
timeout: 5000
database: "<%=ENV['OPENSHIFT_APP_NAME']%>"
username: "<%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>"
password: "<%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>"
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
development:
<<: *default
#database: db/development.sqlite3
test:
<<: *default
#database: db/test.sqlite3
database: testyourcode_test
production:
<<: *default
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
#database: db/production.sqlite3
I setup the RAILS_ENV=development, but i can't figure out how to solve. Any help would be greatly appreciated!
At first glance it looks like you're using the Openshift DB as your default and then all your Rails environments are using that default setup. Your local machine (where I'm assuming you're running the rake db:migrate) isn't able to use those connection details to the OPenshift DB.
Instead of having the Openshift DB configuration in the default section move it down to the production setting and switch the RAILS_ENV=production with rhc set-env RAILS_ENV=production. Then configure the development with settings that your localmachine can use. After thats all done, try running your db:migrate once more.

Database connection on Heroku

Wow I've been stuck on this one for days. I'm having trouble connecting to database.yml on Heroku. I'm on Cedar and ruby 1.9.2. My dev and test dbs are sqlite3 and the prod db is postgreSQL to cope with Cedar rules.
Here is the code in my ruby script:
Rails.env.production? ? (env = "production") : (env = "development")
dbconfig = YAML::load(File.open('config/database.yml'))[env]
ActiveRecord::Base.establish_connection(dbconfig)
All goes well in local but when I push to Heroku, I get:
ArgumentError: syntax error on line 17, col 0: `adapter = uri.scheme'
from /usr/local/lib/ruby/1.9.1/syck.rb:135:in `load'
It looks like Heroku doesn't like my database.yml. Here's an overview:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: foo
port: 5432
host: foobar.amazonaws.com
username: foo
password: bar
First, Heroku overwrites your config/database.yml with its own Heroku-specific version. That's how Heroku automatically connects your application to its own postgresql databases. To tell Heroku about your own posgresql database, you should set up the correct config variables and you might as well omit the production database from your the config/database.yml in your repository because Heroku will ignore it anyway.
Second, the config/database.yml file is an ERB template for a YAML file. You must first run the file contents through Evaluated Ruby (ERB) before running the the output through YAML.

Resources