Error when creating database record on rails console - ruby-on-rails

I'm trying to create a new record through the rails console but I'm getting the following error:
PG::ConnectionBad: FATAL: database "my_database_development" does not exist
I've recently changed from Sqlite3 to PG to be able to deploy to Heroku. Is this what is giving the error?
Thanks a lot!

It looks like you have not yet run
rake db:create
This will "create" the databases on your PostgreSQL server. A step that you didn't have to do with SQLite, but Postgres requires it. As TK-421 said, make sure that your database.yml is configured for your OS and Postgres, not SQLite.
Here is a (possibly outdated) Railscast on the topic.
http://railscasts.com/episodes/342-migrating-to-postgresql

Is this running locally, or on Heroku? In config/database.yml, the local user you specify will need to have CREATEDB privileges (as a superuser, for example).
There's a little more configuration required in this case than for SQLite. Google will show you a bunch of tutorials specific to your OS.

If you see this problem in production on heroku, start the console like this:
rails console production
If you have that problem in development on your machine, check your config/database.yml. It is points to a local development database that does not exist.

Related

warning: circular argument reference - now rake aborted! on rake db:migrate

On rake db:migrate, I am getting following error.
ruby-2.2.1/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
My rails gem gem 'rails', '4.0.2'
What's the solution for this?
From your error, it sounds like you are trying to use a Postgres database with your rails project. Basically, postgres runs separately from your rails project. You need to make sure you have:
a) installed postgres using something like homebrew
b) found a convenient way to start your postgres server when you need to (I recommend the one on postgresapp.com)
c) actually started the server before running the command that's producing this error (e.g., through opening the postgresapp or in a separate terminal window)
Conceptually, think of it this way: you can't migrate (e.g. 'change') a database that's inaccessible because the postgres server is down. That's what the error means.
As an alternative, you can use SQLite instead of Postgres. SQLite does NOT run as a separate process (https://www.sqlite.org/serverless.html), so you will run into these problems less. However, in your ruby code, you will need to pay attention to exactly how to configure your app for a postgres database or a sqlite database, whichever you choose. Here is a great SQLite tutorial for initial set-up:
http://www.integralist.co.uk/posts/active-record.html

I cannot deploy my newly created app on heroku because of postgresql

I'm learning Ruby on Rails, and having lots of difficulty setting up.
So I have created this new app, using 'rails new' command.
Modified a bit, and now on way to deploy it on heroku.
First try, it failed because heroku required the app to use postgresql rather than sqlite3, which was set as a default database.
So I downloaded and installed postgresql, and here is the where the trouble began.
After installing the postgres, I set up the path.
And then I was unable to run the app locally anymore.
In command, "rails server" command is not working anymore, showing all kinds of error not being able to load stuff.
It looks like this:
D:\ruby\appname>rails server
c:\RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1-x86-mingw32/lib/pg.rb:10:in 'require': cannot load such file --2.1/pg_ext <LoadError>
and another bunch of load error.
No need to mention deploying on heroku, it fails everytime.
After installing postgresql, I can't use any database related commands as it says "loaderror" everywhere.
I can't run the app locally, nor deploy on heroku.
How should I fix this database problem?
In development you can still use SQLite, and in production use Postgres.
It's not advisable forever to have that disparity, but it does mean you an make progress.
Remove any production settings in database.yml, but still include 'pg' in your Gemfile.
Heroku will automatically insert database settings for Postgres in production. Deploy and then run heroku run rake db:migrate to set up your database.

Can't run Ruby on Rails tests on Heroku's hosted Postgres databases

I'm currently evaluating Nitrous.io and liking what I'm seeing so far. I've currently got a few databases for testing and development running on Heroku's hosted Postgresql service as well. I'm running into some issues when running tests though, and I'm hoping somewhere here may have a solution.
When I run rake db:test:prepare I'm getting the error:
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
From what I've read elsewhere, that's trying to DROP the database, but Heroku's hosted databases don't allow that. Does anyone out there know how to run Rails tests on Heroku's Postgresql?
Nitrous.IO has released a package manager which will allow you to install Postgres within your Nitrous box. This can be used for your test database as well as your development database if needed:
https://github.com/action-io/autoparts
This also requires that you are using a Nitrous box is running version "bran" or later (see README). You may need to terminate/create a new box if you are running on version "arya".
To use autoparts, run parts search to see all of the packages available.
To install postgresql within the Nitrous box, run the following command:
parts install postgresql
Make sure that your config/database.yml file explicitly sets host: localhost for each database you wish to connect to, or else the pg adapter will fail to find the socket it needs to connect to.
According to the Heroku docs, you need to use "heroku pg:reset" in your rake task.
https://devcenter.heroku.com/articles/rake

How to switch my Rails app to postgresql from Sqlite3?

So I started working on a Rails app recently and we decided (well not me, the person working on it with me) that we should switch from Sqlite3 to Postgresql. I've installed Postgresql on our server properly, created the databases for dev, prod, and test, and updated my Gemfile and database.yml files with the code for Postgres. The thing I'm unsure of now, is how to switch out all the files in the db directory with the Postgres databases. Do I just delete the contents of the db directory in my app and run rake db:create?
You'll want to edit config/database.yml to use postgresql instead of sqlite.
The migrations in db/migrate/*.rb are hopefully cross-database compatible, and wont need to be changed.
Running rake db:create db:migrate with the new database.yml should create the PostgreSQL database and you'll be up and running.
In reality, you'll probably run into various problems, but this will be a starting point.

Welcome aboard ActiveRecord::ConnectionNotEstablished

I'm using Ubuntu, with Rails 3.0.1 with mysql2 socket.
When i do runing install, rake db:create and after rails server, my Welcome aboard, shows ActiveRecord::ConnectionNotEstablished in About your application’s environment
What i do?
Had the same problem on rails 3.1.1:
rake db:create - ok
rails console and some DMLs - ok
but accessing info from the web-page resulted in ActiveRecord::ConnectionNotEstablished.
A rails server restart helped.
It sounds like your MySQL server isn't running. You'll need to install MySQL if you haven't already (apt-get install mysql-server should do it). Once it's running, you'll need to set up a user and database for your app, and note the username and password so that you can put that information in config/database.yml within your app.
This link will be useful if you need any help with those steps.
You'll need to do some more debugging to work it out.
How are you running your server?
Make yourself a model.
rails generate model Something name:string
Then try running rake db:migrate
Does this work?
If it does, then you must be running your server in a different way (maybe you're running it in production mode?)
Try rails console and try to do Something.count
If all of those work
then I'd suggest you try restarting your server.
rails server

Resources