NoDatabaseError when running Ruby Rails App - ruby-on-rails

I'm using Ubuntu version 15.10. After I type rails s in the terminal to start a Ruby app and navigate to 0.0.0.0:3000, I get the following error on the web page:
ActiveRecord::NoDatabaseError at /
FATAL: database "local-election_development" does not exist
Run $ bin/rake db:create db:migrate to create your database
When I type bin/rake db:create db:migrate from the terminal, I get this error:
PG::InsufficientPrivilege: ERROR: permission denied to create database
I found this answer, but I did not know how to execute the command ALTER USER new_user CREATEDB;.
How can I solve this error?

You can to enter the SQL command on the PostgreSQL console, which can be invoked with psql.
If it complains that the database does not exist, then use createdb, if the user doesn't exist, use createuser. Search for PostgreSQL-related questions if you have an issue with any of these.

Log into the postgres user:
su - postgres
Log into by typing:
psql
You should now see a prompt for postgres=#

Related

ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations

I have a local project, with rails and postgres. I threw it up on my AWS Amazon Linux AMI. I have run test projects with rails and postgres on the server. However when I uploaded my local project, and try to run
rake db:migrate
I get the following error:
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations
I saw some similar issues, but none of them help. I do have the proper role setup and connected.
I'm not sure if you're running rake db:migrate with RAILS_ENV as production or development. Whichever it is (development by default), in your config/database.yml it will say the user, password, and database it is running on. That user must have ALL privileges on the public.schema_migrations table. If it does, and it's still not working, make sure that user has ALL privileges on the public schema.
Read more about manipulating postgres database privileges here. Postgres has excellent documentation.
One more thing: if you're creating this database locally, and trying to create the initial database instead of actually running a migration, use rake db:schema:load instead of rake db:migrate. Never run this on production, as it will delete your data!

Rails - FATAL: database "myapp_development" does not exist

I am trying to set up my Env to use PostgreSQL with Rails and i followed the steps to install PostgreSQL from this Article
Am getting the following error
ATAL: database "myapp_development" does not exist Extracted source
(around line #661):
rescue ::PG::Error => error
if error.message.include?("does not exist")
raise ActiveRecord::NoDatabaseError.new(error.message, error)
else
raise
end
Did you run rake db:create and rake db:migrate before rails server?
UPDATE
Here's all the steps you should do:
cd /your/app/path
bundle install
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rails server
Run
rails db:create
before running
rails db:migrate
You can also create PostgreSQL database manually by using psql command prompt.
When in there, connect to your local server and write "create database myapp_development;" without the quotes
I was having a similar problem. I checked different websites and tried what they suggested but didn't work. Then I tried rake db:create:all and rake db:migrate it worked for me. Thank you!
You can fix it by:
bundle exec rake db:setup
In my case, the solution was to provide what the error said.."create the database that does not exist"....I spent many hours trying different commands or run db:migrate using bundle but none of that worked. I thought it should create the db on it's own but no...I eventually decided to create the db in postgres
sudo su - postgres
psql
CREATE DATABASE tableNameFromDatabase.yml WITH OWNER myusername;
after this rails db:migrate worked

Heroku Schema load: Permission denied for database "postgres" User

I'm trying to deploy my Rails app to Heroku, and I now want to load my schema. I get the following error:
Running `bundle exec rake db:schema:load` attached to terminal... up, run.1533
rake aborted!
PG::ConnectionBad: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Which is weird because postgres is not the database it should connect to. The database URL, which is available to the app via ENV, has the right values, and the long apparently random name d3kwlkwlwjwljetc....
Searching for this message gives me people who are trying to drop or reset the DB, which is not what I'm doing.
I have 0 rows
I tried pg:reset, even removed and put back the database. No avail.
I'm not doing db:drop, just db:schema:load
I'm dumbfounded. Why can't my app connect?
Edit: Apparently, I can't schema:load, but I can migrate all my migrations over. Why is that?
It appears that this is an issue in Rails itself - there is a discussion regarding it at https://github.com/rails/rails/issues/17945 - it seems that there's a commit (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb#L77) that is connecting to the posgres database which isn't permitted on Heroku so fails.
In short, for the moment use db:migrate until the issue is resolved.

Trying to migrate database for PostgreSQL, using Ubuntu, running into issues with "permissions" and "roles"

I recently installed Ubuntu and PostgreSQL. I'm trying to pull up my PostgreSQL database. If I type rails db I get this error
psql: FATAL: database "homed_in_development" does not exist
So I then try running rake db:create or rake db:migrate but I get the error
PG::InsufficientPrivilege: ERROR: permission denied to create database
So I have tried CREATE ROLE myusername; after getting into PostgreSQL through the common psql postgres, which gives me the error ERROR: permission denied to create role. I tried to fix this issue using ALTER USER myusername WITH SUPERUSER, but I got the error message ERROR: must be superuser to alter superusers
I also got the error FATAL: role “myusername” does not exist on something else, and so I think my issues are that I haven't got roles or permissions set up properly.

Ruby on Rails Rake Error

EDIT: Solved the problem, thanks to this forum post: http://forums.aptana.com/viewtopic.php?f=20&t=7563&p=27407&hilit=libmysql.dll#p27407. Thanks everyone!
I've started learning RoR and have been trying to use rake db:migrate but I keep getting the same error. I can connect to the MySQL database using C:\dev\railslist>mysql -u root railslist_development -p.
rake db:migrate --trace produces the following:
C:\dev\railslist>rake db:migrate --trace
(in C:/dev/railslist)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
Mysql::Error: query: not connected: CREATE TABLE 'schema_migrations' ('version'
varchar(255) NOT NULL) ENGINE=InnoDB
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract_adapter.rb:219:in 'rescue in log'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract_adapter.rb:202:in 'log'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/mysql_adapter.rb:323:in 'execute'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract/schema_statements.rb:114:in 'create_table'
...
My database.yml file is as follows:
development:
adapter: mysql
database: railslist_development
username: root
password: **********
host: localhost
...
EDIT: Sorry, I got mixed up there... I can connect to the MySQL database using mysql connect localhost - it produces a long list of commands and variables. Also if I enter mysql -h localhost -u root -p I can log into the MySQL prompt. So to clarify: I can connect to the MySQL database via the command line, however in RoR Rake produces an error.
To clarify here - the problem is that the client MySQL library does not work currently with Rails 2.3.
To fix this:
Download an older version of the client library here
Drop this file in your ruby bin directory
Restart your MySQL service
[1]: [1]: http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll "here"
I'm not answering your question per se, but since you're just learning, why not stick to the simpler sqlite for now?
The DB is not created.
When using MySql, before running rake db:migrate you should create the DB doing:
rake db:create
Or create the database manually via SQL commands.
mysql -h localhost -u root -p
> CREATE DATABASE railslist_development;
I hope this helps you.
This is almost definitely because your mysql instance is not running or you haven't configured config/database.yml to be pointed at the right database for your environment (usually Development). Here are a couple of things to try -
Check your config/database.yml -
where is your host (if no host
listed, it'll connect to localhost)
Try running mysql -h localhost and
see if mysql is running.
EDIT:
If you're not able to connect to your localhost database, then the problem is there, not with Rails. Make sure you have it running, and that your permissions are set correctly to allow connection from your machine. Also, try connecting as root from your local machine, to see if it's a more granular issue (such as you have local connections enabled, but not for the user you're using in Rails).
EDIT 2:
In this case, your problem likely is that your database has not been created. Simply go to a command line and type the following:
mysql -u root -p -e 'create database railslist_development;'
This should create the database and allow you to run your migration.

Resources