mongoid/mongodb and rake task authorization in production/passenger - ruby-on-rails

When trying to run rake db:seed on my app, I get the error:
Database command 'count' failed: {"assertion"=>"unauthorized db:app_development lock type:-1 client:127.0.0.1", "assertionCode"=>10057, "errmsg"=>"db assertion failure", "ok"=>0.0}
I get the same error with db:drop
Mmy app is connecting to the DB fine, it's just these rake taks are failing, and I'm not sure wh. I dont think seeding should need admin privileges. Maybe it's a passenger issue?

You need to have the username and password set in mongoid.yml for rake tasks to be able to connect to your database.
Example:
production:
host: host-name
port: 27017
username: itsmeyo
password: supasecret
database: project_production
You can also have them set in environment variables, but they need to be set.
password: <%= ENV['MONGOID_PASSWORD'] %>

Related

Postgres db not working on Heroku

I've spend several hours figuring out how to get my database up and running. I created a new rails app and wanted to deploy it to heroku. I followed the instructions from heroku (to switch from sqlite3 -> postgresql) but it just doesn't work.
This is in my database.yml file:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
url: <%= ENV['DATABASE_URL'] %>
I can't create or seed any data in the database. Sometimes it executes the db:migrate, but even then it doesn't create anything. This is what I get when running:
heroku run rake db:create
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Does anyone has an idea on how to solve this? I don't have a clue anymore ...
Thanks!
By default you don't need to create a db on heroku.
Just run heroku run rails db:migrate and rest of the stuff will be handled by heroku itself.
Also your database.yml should be changed to following for Production env.
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Also your rails app is by default assigned with a Postgres addon.
You can check this by running command heroku addons in the console.
Source -
Heroku Getting Started Guide
Heroku Postgres Addon Guide
You cannot create a database on Heroku using db:create (you cannot drop it neither). Your database is created when you add an add-on (such as Heroku Postgres). You can only migrate and seed. And if you want to start over, you can use pg:reset (instead of drop and create)
So the correct sequence should be:
add the Heroku add-on (such as Heroku Postgres). Add-ons are located here: https://elements.heroku.com/addons.
rake db:migrate
rake db:seed
if you want to start over
rake pg:reset
rake db:migrate
rake db:seed
From Heroku documentation: https://devcenter.heroku.com/articles/heroku-postgresql
The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.
As per the given stacktrace, it seems like you are trying to create a database on heroku which in turn is giving you Permission Denied Error.
Firstly, you do not need to run
heroku run rake db:create
Instead run
heroku run rake db:migrate
and it should migrate the migrations which are down.
For checking the current status of migrations run the following command:
heroku run rake db:migrate:status
Other Point you mentioned:
-> I can't create or seed any data in the database
As already mentioned above you can't create a database as heroku does it for you .
For seeding data in the database run the following command:
heroku run rake db:seed

Setting up PostgresQL on Heroku Rails 4

its been a while since i have used postgresql and deployed an app to Heroku, think I have made an error somewhere with my setup.
I have created my app on Heroku, there is a Hobby Dev database setup (I ran Heroku run rake db:setup) which set up my database, but I'm wondering in my database.yml file do i have an error
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
When I run heroku run rake db:create i get
FATAL: permission denied for database
DETAIL: User does not have CONNECT privilege.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"databaseName", "username"=>"userName", "password"=>"password", "port"=>5432, "host"=>"hostNamehere"}
I have set the database password using heroku config:set
What have i missed here?
Thanks
Heroku ignores your database.yml configuration, they generate one when you deploy your application and they also take care of database creation.
All you need to do is heroku run rake db:migrate and maybe a heroku run rake db:seed in case you need to seed your database.

postgresql database migration error: PG::ConnectionBad: fe_sendauth: no password supplied

I am trying to run rake db:migrate in my rails app, but getting the following error:
PG::ConnectionBad: fe_sendauth: no password supplied
My database.yml is as follow:
<%
branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
branch = 'master' if branch.empty?
suffix = branch == 'master' || branch.starts_with?('hotfix') ? '' : "_" + `git config --local --get offgrid.databases.#{branch.gsub(/[^a-zA-Z0-9]/,'')}`.strip
suffix = ENV['DB_SUFFIX'] if ENV['DB_SUFFIX']
puts "Database is 'offgrid_dev#{suffix}'" if Rails.env.development?
%>
development:
adapter: postgresql
database: offgrid_dev<%= suffix %>
host: localhost
encoding: unicode
pool: 5
password: 1
test:
adapter: postgresql
database: offgrid_test<%= suffix %><%= ENV['TEST_ENV_NUMBER'] %>
host: localhost
encoding: unicode
pool: 5
password: 1
the more confusing thing here is that when I try migrating my test database as follow:
rake db:migrate RAILS_ENV=test
my test database gets migrated successfully, without giving the password error.
this is very confusing, as I have more-or-less the same configuration for both test and development environment.
My first question is why is this so?
More importantly though, How do I get to overcome this?
I have tried to change password to be nothing, by using the psql console to set the password as: \password <username>, and hitting return with no input for new password, and confirmation, and then running the migration, with no success. I have also tried to run the migration while specifying the environment as: rake db:migrate RAILS_ENV=development with no success as well
What am I doing awfull, and how can I get this up?
Big thanks for all help.
PS: my OS is ubuntu
Whao... after searching more on stackoverflow, I got this question, which points to this documentation.
On following that, and configuring my .pgpass file with default username and password as said in the documentation, all worked fine!!!!
Although, I still do not understand why it was previously working for my test env database, but not the development env database.

Connection refused - connect(2) with rake db:seed on Mongodb

I am using rails 3.2 and mongoid.
I make these steps for setting my database:
// Add an Admin User (to the admin db)
use admin
db.addUser("theadmin", "anadminpassword")
// Use your database
use superuser
// Add a user (to your database)
db.addUser("John", "passwordForJohn")
// show all users:
db.system.users.find()
// add readonly user (kinda cool)
db.addUser("readonly", "passwordForJohn", true)
In my mongo.yml I have:
production:
host: localhost
port: 27017
username: John
password: passwordForJohn
database: namedatabase
You can see in http://www.mongodb.org/display/DOCS/Security+and+Authentication
My problem is that now I try run:
rake db:seed
I get now this error:
rake aborted!
Connection refused - connect(2)
How can I fix it?
The problem was fixed:
The problem was with solr server:
If you have solr server in your project:
1º Make sure that the Solr server is started before you seed:
rake sunspot:solr:start
rake sunspot:reindex
2º Run your seed:
rake db:seed
Regards

Why is PostgreSQL rejecting my password when I try to migrate?

I have PostgreSQL installed via this link and all seems good. I can use the GUI interface and I can see my databases but when I run rake db:migrate I get the following error:
$ rake db:migrate
(in /Users/tamer/Sites/sample)
rake aborted!
FATAL: password authentication failed for user "tamer"
(See full trace by running task with --trace)
Here is my database.yml file:
development:
adapter: postgresql
database: test
encoding: unicode
host: localhost
user: postgres
password: mypass
timeout: 5000
Where "mypass" is my password I set.
Here is a picture of it running
Any ideas what i am doing wrong?
Well something doesn't quite add up. The error message refers to a user "tamer" but the database.yml file has the "postgres" user. Are you sure this is the database.yml file that's being used? What happens when you try to log in with the GUI tool with either user?

Resources