Everything works except rails console after moving to Postgresql on Cloud9 - ruby-on-rails

I've moved my development environment from sqlite to postgresql on Cloud9 IDE, following this post Cloud9 postgres.
Now just about everything works,
created a new db
migrated table changes
saved data to db with rake tasks
rendered data from the db in views
But when I run rails c and try and load data there, it fails.
Item
=> Item (call 'Item.connection' to establish a connection)
Item.all
=> PG::ConnectionBad: fe_sendauth: no password supplied
I don't think its actually a no password issue because in every other way I can access and work with the database... just not in rails console.
Does maybe something need to be done to setup a new connection here?

the main issue is POSTGRES need a role to be setup , no doubt the app must be working fine, but when u type in rails c , the control access the schema file and tries to load all the table, here the table ITEM must be the first one to be read by control.
all you need to do is create a role in postgres as superuser and type in its credentials in database.yml.
let me know if this works

Related

"ActionView::Template::Error (Unknown primary key for table" after trying to push local database to Heroku

For a Ruby-on-Rails app that I'm hosting through Heroku, I recently downloaded a backup, restored it locally and then added data to the database from outside sources. That all worked fine.
After the updated database was pushed to Heroku using heroku pg:push <localdbname> HEROKU_DATABASE_URL --app <appname> the app was working fine and I could see the data that was newly added.
However today when I tried to log in to the app on Heroku, I was getting error messages. In my log file I saw this error:
ActionView::Template::Error (Unknown primary key for table ...
According to this SO post: Getting "Unknown primary key for table" while the ID is there
one user was able to get around this problem by resetting and pushing the database to Heroku several times. However, that hasn't worked for me. I've tried to reset and push the database at least 4 times now.
One possibility is that my local postgres database is using version 9.6 and the app is using version 9.4. The Heroku database is a Hobby-Basic database. There is documentation from Heroku on how to upgrade the Postgres version but it's not totally clear from their example what I would need to do. I'm guessing that I basically need to create a new database, copy the data from the old to the new and then destroy the old. Has anyone done this before? Is there a fee associated with doing so? And has upgrading the version of Postgres remotely fixed this issue for other people?
I have ran into similar issues before. I was creating a postgres database outside of rails in RazorSQL. I imported data from outside sources and when I ran it locally everything worked fine. When it came to deployment I ran into all sorts of issues.
I created tables outside of rails thus no migrations were created. I had to recreate the tables by deleting the current model and generating a new model which is a copy of the old one only this time a migration is created for example:
rails generate model ad name:string description:text price:decimal
seller_id:integer email:string img_url:string
I needed to import data from the database I had already created and before I did anything I actually created a seed file using this gem https://github.com/rroblak/seed_dump. All I had to do when I created the model again (remember to delete the model and recreate it) was run rake db:seedand it pulled the data in.
3.The last thing I had to do was insert the assocations in the models for example in a owner model putting in has many: customersto connect the customer model.
Another scenario was I ran a query in RazorSQL and generated a new table from the query and imported it into postgres. The problem was it was only a table from results thus it did not have a primary key. I had to manually create that in postgres using ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY; This gave me an auto incrementing primary key field and I have to give credit to this answer which helped me https://stackoverflow.com/a/2944561/7039895.
Hope this helps.
According to the docs, you several choices of how to upgrade your database. pg:copy and pg:upgrade.
In your case, I recommand pg:copy: this is easier but required your database to be "off" while your doing the upgrade. This should not be an issue since your database is not currently working.
pg:upgrade should only be used when the downtime required for a PG copy
upgrade is unacceptably long for your business.
Upgrade with PG copy: (All the steps are explain in depth in the link)
Provision new database
You need to create a new database, it will be automatically using the last version of postgresql for heroku (in your case 9.6)
heroku addons:create heroku-postgresql:standard-0
Prevent new database updates
Stop you current database from writing mode to avoid corrupted data while copying to the new
heroku maintenance:on
Transfer data to new database
You need to copy all the data from the old DB to the new.
heroku pg:copy DATABASE_URL HEROKU_POSTGRESQL_PINK --app sushi
Promote new database
You need to notify heroku that you will use the new DB and not the old one.
heroku pg:promote HEROKU_POSTGRESQL_PINK
Last step: Make application active
Everything should be good by now, just make your DB active so it can save new query.
heroku maintenance:off
As you can see, all the steps are straightforward. Therefore, if upgrading you version of postgreSQL doesn't solve the issue, you can still switch back to the old one before removing it.
Pricing
I think it depends of your subscription already (Hobby, Standard or Premium) https://www.heroku.com/pricing. It may be free, or cheap, but I think it also depends of the size of your DB.
To be sure it won't cost you too much, I'm sure you can ask directly to Heroku support https://devcenter.heroku.com/articles/paid-support.

Is there any chance of testing the PostgreSQL database in this scenario without duplicating the database in the development environment?

“I have a Rails application with PostgreSQL database. My database contains 1000 records in it.
I have written a rake task which will manipulate the records in the database.
If the rake task went wrong i want to revert back the changes to the original in the database.
is there any chance of testing the database in this scenario without duplicating the database in the development environment ?”
In Rails itself having the feature sandbox
If you wish to test out some code without changing any data, you can do that by invoking rails console --sandbox.
bin/rails console --sandbox
Loading development environment in sandbox
Any modifications you make will be rolled back on exit
irb(main):001:0>
You can paste your ruby code here and can see the changes you wanted to see. while closing this console, All your changes will be reverted back(running as a transactional)
For more informations you can refer this link
http://edgeguides.rubyonrails.org/command_line.html#rails-console
you can duplicate a database in same env.
create database test with template original_db;
Will create cop of existing db, on which you can run your tests, specifying test instead of original db name in connections string

rails c not using the same database as rails db

I just realized that when I run rails c I'm getting a different database than when I run rails db.
I initially though the console wasn't connecting to the database at all because it kept returning empty sets, for instance when running User.all, despite having seed data. I then inserted data into the database via the console and did a query from the SQLite console. The new data did not appear even after reloading the console.
My confusion is further exacerbated by the fact that I only have the db/development.sqlite3 file and rails r "puts Rails.env" returns Development.
So where might I have gone wrong here? Why is one using the development database and the other seemingly using something else? Could rake db:seed be pushing to Test perhaps? Is there a way to check the SQLite console for the environment?
Turns out Spring was causing the problem. I had a previous app with the same name and it was loading that when I opened the rails console despite being in the newer app directory.
I've disabled Spring with the DISABLE_SPRING variable and now things are working as expected.

Location of postgresql database in nitrous.io project

I'm not sure if I'm going mad or if Heroku's dataclips are acting up but I have the following problem.
I've set up a ruby on rails development environment on Nitrous.io and have connected to a database on Heroku. In Nitrous.io I then type psqlto access postgresql.
From there I type \lto list the databases.
Then \c <name of development database hosted on heroku>
Then \dt to list the relations, one of which is users
Finally, I input the command SELECT * FROM users; and this returns relevant information on users such as email address, encrypted password etc etc.
However, when I go to Heroku and create a dataclip with the code select * from usersto be run against the same database, I'm getting the following warning
Error: Dataclip cannot be created
ERROR: relation "users" does not exist
LINE 2: select * from users
Am I completely missing something here or is the dataclip throwing an error for no reason? I'm using Devise (for the first time) in my app. Would this have anything to do with it?
Edit
I'm starting to think I've stored the database on Nitrous.io and not in Heroku. I think I may have used autoparts to install postgresql on my nitrous.io vm
I've run the command SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' as a dataclip on Heroku and the users table just isn't there. I then ran it on some other projects I have on Heroku and I can clearly see the tables I've created.
Can anyone confirm my suspicion or know how I can check the location of the database?
Yes I had indeed set the postgresql database up in nitrous.io itself rather than on Heroku. This page explains it: http://blog.nitrous.io/2013/07/02/building-a-rails-4.0-app-on-nitrous-io.html

Rails DB Migration - permissions

I'm a Rails novice, but am currently trying to modify an existing application that I inherited.
I'm running some db migrations that create new tables on our staging server (Not local machine) and all the new DB tables create seem to have different permissions than the tables already created.
I am running "rake db:migrate VERSION=20110213100531 RAILS_ENV=staging" with one sshlogin and I have my sql login defined in my database.yml under staging as separate mysql database login.
Is there any particular settings/user I should be running my db migration under?
From what you've said I think most likely your problems are being caused from the way you have your user permissions set up in the remote database. This is not something that can be altered from the rails side.
Your database.yml file will just specify your login details into that database, but it's up to you to modify your user permissions settings in your database.
If you've got admin access into your database, I'd start by comparing the previous user's permissions to your own and seeing what's the difference.
All the best

Resources