Rails project can't connect to postgresql - ruby-on-rails

I was trying to create a rails project with postgresql
I use this
>>rails new ReadingList --database=postgresql
>>rails s
However, it shows this error
Then, I jump to psql and create readinglist_development by myself, but it still show the same error.

In Rails practice, run rake db:create to create the database after creating new Rails project.
I believe the name of the database(readinglist_development) is not matching the database name of ReadingList_development as in database.yml which Rails does not recognize the database created using psql.

Its better not to create databases form db workbenches, better to use rails db commands.

Related

Rails didn't create any db directory

While I'm creating a new rails app, there is no db directory while skipping active record for using mongodb later, By this command.
rails new app-name --skip-active-record
where rails new app-name created db as expected. And so, rake db:create or rake db:migrate don't act expectedly. I need to edit /db/seeds.rb & i cann't find it.
I tried some post fixing, nothing brings the missing directory. I'm new in rails. Have I missed something? Thanks for cooperating.
By including --skip-active-record in the new command, you are telling Rails to not generate the database files, which is correct if you want to use MongoDB!
Next, you need to remove the sqlite gem from your gemfile and add the MongoDb gem to the gem file. Here is the Ruby Gems page for MongoDB, just copy the gemfile reference from there and paste it into your gemfile. You always need to run the bundle command after altering your gemfile
Then you run rails g mongoid:config and rails will generate the config/mongoid.yml for you.
Here is an article for reference: https://gorails.com/guides/setting-up-rails-4-with-mongodb-and-mongoid
MongoDB does not have a schema so you don't need migrations. Thats kind of the whole point of a schema-less database.
db/seeds.rb is just a plain Ruby file thats executed when you run rake db:seed*. You can create it by running $ touch db/seeds.rb.
There is nothing magical about it.
If you are using Mongoid then everything you need is generated when you run:
$ rails g mongoid:config
So just relax.

Error when creating database record on rails console

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.

Generate rails db migrations files from active database

Is there any way to take a running database and generate a migration file from it? If not does anyone have any advice on how to approach that?
Background: Have a new project where a PHP developer jumped into a rails project and starting adding tables and columns though PostGres admin tool.
Created a directory called "log" then ran this command
RAILS_ENV=production rake db:schema:dump

why is rails telling me to run rake db:migrate?

Hello I am new to rails.
I am working with an existing postgres db. I would like to create CRUD for all of the tables.
I've started with:
rails g scaffold firstTableName
Then I start rails:
rails s
When I navigate to localhost:3000/firstTableName, rails gives the following error:
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
I thought that 'rake db:migrate' was only for setting up your database. My db is already set up. If I run 'rake db:migrate', then I get an error that firstTableName already exists.
Please Help me understand how to make rails happy.
Thank you.
the error is because you already have the table firstTableName is already present and as you did scaffold, it will create the model, controller and view for you, so it has also create the migration for the firstTableName.
If you already have the migration in place try skipping the migration while scaffolding
rails g scaffold firstTableName --skip-migration
Your database should have a table named schema_migrations with only one column version. Then you should write all migration versions into that table by hands.
Migrations placed in project_root/db/migrate/, and the вшпшеы in the file name is the same as version migration.
When you run migrations rails keeps track of these by matching each migration in db/migrate folder with an entry in schema_migrations table that includes timestamp for all the migrations that rails has already run for that environment in versions column.
Reason is to allow you to migrate incrementally.
Earlier versions of rails had a strong philosophy of having a down migration as well to go back in time. However, that has been left as a choice now.
Your complaint is because it was not able to create schema_migrations table. My suggestion is to allow it to do so by running rake db:migrate on a fresh db.
If you have data in the tables on this instance run it on another database and then just copy across the versions table.
http://guides.rubyonrails.org/migrations.html

Can't recreate postgres db in rails app

So, had a working postgres db instance with my rails app on my development Mac, but the migrations were getting unmanageable as I'm building the model up from scratch (i.e. adding, removing, changing columns), so I decided to start clean. After running rake db:drop, I can not recreate via rake db:create. I simply get the following:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"xyz_development", "pool"=>5, "username"=>"xyz", "password"=>nil}
I am thinking I need to create the user in postgres, but shouldn't it be there from initial setup?
Thanks.
You should see the SQL statement to create the database in the log output, and probably also the error. Check that.
Also check that the database doesn't already exist. Try creating the database manually using psql(1) with the username/password you use in rails and see what happens.
$ createdb xyz_development
Use the postgres bin createdb

Resources