Configure database with sqlite3 for Rails and postgreSQL for Heroku - ruby-on-rails

I looked for hours on the web but none of my attempts to adapt my database.yml work out. I just started with Rails and the exercise is to use sqlite3 for development, and postgreSQL for production. I am using rvm 2.4.1 and rails 5.3.1.
I added this to my gemfile:
group :production do
gem 'pg'
end
And this is my database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
So that does not work out and I get the following error when I try to deploy to heroku:
Specified 'postgresql' for database adapter, but the gem is not loaded. Add
`gem 'pg'` to your Gemfile (and ensure its version is at the minimum required
by ActiveRecord).
I am assuming I need to somehow let the production part lead to postgreSQL but the manuals on the heruko website and other posts on stackOverflow and elsewhere are not helping me. How can I get it work? I can add :default to the groups, gem 'pg' is in within my Gemfile but then other things like scaffolding produce errors later on. I would really be thankful to get specific help on this as a spend about 12 hours on this single issue already..

first, since you informed in your production using postgresql then you must change setting in database.yml (see production part)
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
adapter: postgresql
encoding: unicode
database: project_name_production
pool: 5
host: localhost
username: your_username
password: your_database_password
and then you can follow this rake command in heroku
second about your gem file, if the problem still persist then you can put out from production environtment and push to heroku, and then do bundle install to install the adapter
GEMFILE
gem 'pg'
here is good for your to read to solve your problem link information for heroku with postgresql

Related

rake db:create - rake aborted

Hi im trying to follow this link to create a scraper and I am stuck trying to create a db
https://towardsdatascience.com/job-board-scraping-with-rails-872c432ed2c8
I get this error when typing rake db:create I've tried creating inside and outside the folder and I get this error I am not sure why, thank you very much. I have been closely following the link so I have all the same files and am sure everything was installed properly.
rake aborted!
ActiveRecord::DatabaseConfigurations::InvalidConfigurationError:
'{ default => }' is not a valid configuration. Expected '' to be a URL string or a Hash.
This is my database.yml file
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
test:
<<: *default
database: scraper_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
Thanks in advance everyone!
Update
Thanks for the pointer I had [the database.yml file] indented but I though an error told me to remove tabs. new issue now:
could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Couldn't create 'scraper_development' database.
Please check your configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"
The file is in YAML format. The sub-items must be indented.
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
You can add " gem 'pg', '>= 0.18', '< 2.0' " to the gem file and make the necessary settings in the YAML file.
I ran into the same problem and what I did was to fix the indentation, and then it worked.
The database.yml file is in YAML format. so make sure the sub-items are indented correctly. Please check the example below on how you are supposed to indent it.
This is how your database.yml file should look like
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
test:
<<: *default
database: scraper_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>

YAML syntax error occurred while parsing C:/Users/Vanessa/rails_space/config/database.yml

When I enter this code into my command prompt:
rails g model User
Here is what I get in the console:
YAML syntax error occurred while parsing
C:/Users/Vanessa/rails_space/config/database.yml. Please note that YAML
must be consistently indented using spaces. Tabs are not allowed. Error:
(<unknown>): did not find expected key while parsing a block mapping at
line 7 column 1 (RuntimeError)
Here is the code from my config/database.yml file:
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
adapter: mysql
database: rails_space_development
username: root
password: NothingToSeeHere
host: localhost
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
It looks like, under your development key, for adapter, you have a spacing error. Try changing it to:
development:
adapter: mysql
You have one too many spaces before adapter.
YAML is very specific about spacing. As you've found out, even one extra space throws an error. And you have to make sure you use spaces, not tabs.

How to set PostgreSQL as the database for Rails project

My professor said it would be wise to use PostgreSQL instead of SQLite in our project so that it would be easier to deploy to Heroku, but it seems that I didn't set up the database.yml properly.
Here's what I tried to do after I installed the Pg gem. The default database.yml content was:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I decided to change the default adapter "sqlite3" to "postgresql", hence:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
but when I use scaffold I get the error:
Specified 'postgresql' for database adapter but the gem is not loaded. Add 'gem pg' to your Gemfile and ensure its version is at the minimum required by active record
so I'm guessing that I need to put something like gem 'postgresql' in the gemfile. But the problem is I dont know the current version of PostgreSQL I have now so that I can put it on that line.
How do I know the current version of the PostgreSQL? Is this going solve my PostgreSQL setup for my project?
To use Postgres with Ruby on Rails you should follow these steps:
add the pg gem to your Gemfile which is located in the root directory of your application
run bundle install, which will install the gem and set its version and dependencies in your Gemfile.lock
install and configure postgres in your computer
configure the connection uri in config/database.yml by changing
database: db/development.sqlite3
to something like
database: "postgres://username:password#hostname:port/database_name"
In your Gemfile just add pg gem to your production group
group :production do
gem 'pg', '~> 0.19.0'
end
Then run bundle

ActiveRecord::ConnectionNotEstablished for new project

I made a wrong choice and now I am sticked at the middle of my project
I used ruby 5.0.0rc1, and while I start my project with rubymine I get "ActiveRecord::ConnectionNotEstablished"
I also get an error message while I type rake db:create :
Gregoires-MacBook-Pro:p1 gregoire$ rake db:create
Rake tasks not supported by 'pg' adapter
Couldn't create database for {"adapter"=>"pg", "pool"=>5, "timeout"=>5000, "database"=>"pg"}
rake aborted!
ActiveRecord::Tasks::DatabaseNotSupported: Rake tasks not supported by 'pg' adapter
my database/yml :
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: 'pg'
pool: 5
timeout: 5000
development:
<<: *default
database: 'pg'
test:
<<: *default
database: 'pg'
production:
<<: *default
database: 'pg'
I am newbie with ror , any help will be appreciated (please detail)
greg
config/database.yml gives Rails the information needed to access the database of your choice.
The adapter name pg is not valid. If you intend to use PostgreSQL, you should set it as postgresql.
And you have to make sure PostgreSQL is installed and running.
A typical database.yml could be like the following:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
username: your_username_for_postgresql
password: your_password_for_postgresql
development:
<<: *default
database: your_app_name_development
test:
<<: *default
database: your_app_name_test
production:
<<: *default
database: your_app_name_production
username: your_username_for_postgresql_on_production
password: your_password_for_postgresql_on_production
Thanks for your answers, I had multiple problems :
1/ Postgresql was not properly installed AND running
Fresh install using some comments found here :
https://gist.github.com/lxneng/741932
2/ my database .yml was nos properly configured, I created users with
new access : http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
What you gave me as advise made me find the way, also a project 'out of the box' was not runable
Thanks for your time and advises
greg

How to Fix Rails Error: Development database is not configured - ActiveRecord::AdapterNotSpecified

I have been been repeatedly getting this error despite trying to change many things.
I viewed other SO pages and changed indentation, names, etc. Really not sure what's going on. Any insight would be greatly appreciated.
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: lightpath_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: lightpath_test
production:
<<: *default
database: lightpath_production
Try switching back to the default database.yml file.
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
You're likely getting that error because you're trying to use Postgres database without properly configuring Postgres on your development machine/server.
For details on how to switch over to postgres you should watch this RailsCast episode.

Resources