PG::UndefinedTable: ERROR: relation "users" does not exist - Azure database - ruby-on-rails

After deploying a web app on Azure (via Linux Virtual Machine) I successfully access the home page (first page) but get this error when I try to login (top-right of the navabar):
I looked up this issue and I saw other people had it. They solved it by resetting and migrating, but it does not work.
So far I have tried:
The following command correctly executes but does not solve the problem:
RAILS_ENV=production rake db:drop db:create db:migrate
Commenting out the user.rb file but it did not solve the issue
I think the error is due to the gem devise or gem rails_admin, But I cannot work out the solution.
Gemfile :
source 'https://rubygems.org'
ruby '2.6.0'
gem 'grape'
gem 'bootsnap', require: false
gem 'devise'
gem 'jbuilder', '~> 2.0'
gem 'pg', '~> 0.21'
gem 'puma'
gem 'rails', '5.2.2'
gem 'redis'
gem "mini_magick"
gem "chartkick"
gem 'country-select'
gem 'rails_admin', '~> 1.3'
gem 'activestorage-validator'
gem 'rubyzip', '>= 1.2.1'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', ref: 'c8ac844'
gem 'axlsx_rails'
gem 'autoprefixer-rails'
gem 'bootstrap-sass', '~> 3.3'
gem 'font-awesome-sass', '~> 5.5.0'
gem 'sassc-rails'
gem 'simple_form'
gem 'uglifier'
gem 'webpacker'
group :development do
gem 'web-console', '>= 3.3.0'
end
group :development, :test do
gem 'pry-byebug'
gem 'pry-rails'
gem 'listen', '~> 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end
Content of database.yml :
default: &default
adapter: postgresql
encoding: utf-8
database: caoee
username: mat
password: ***
pool: 5
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
adapter: postgresql
encoding: utf-8
database: caoee
username: mat
password: ***
pool: 5
test:
<<: *default
adapter: postgresql
encoding: utf-8
database: caoee
username: mat
password: ***
pool: 5
production:
adapter: postgresql
encoding: utf-8
database: caoee
username: mat
password: ***
pool: 5
Postgresql roles:
List of roles
Role name | Attributes | Member of
-----------------------------------
mat | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS|{}

Are you running that command RAILS_ENV=production ... Locally?
Because that will just run the command locally, with production settings.
You need to reset the database on your Azure server. I'm not familiar with the Azure CLI but I'm sure there's commands to reset the DB remotely.

Related

UPDATED: Sqlite3 gem rails not working on heroku and local env

I am using heroku in a rails blog. After being able to fix the gem file following many related answers here, and being able to push my blog to heroku the app is not working locally.
I have change 'sqlite3' for 'pg' in my production env and add 'sqilte3' only in development test, push my changes to git and after to heroku. So far it was working but when I try to run the rails server, is asking me for 'sqlite3' again.
Error:
rb:177:in rescue in spec': Specified 'sqlite3' for database adapter, but the gem is not loaded. Addgem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
GEMFILE
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'devise'
gem 'will_paginate', '~> 3.0.5'
gem 'cancancan'
gem "paperclip", "~> 5.0.0.beta1"
gem 'ratyrate'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem "sqlite3"
gem 'rspec-rails', '~> 3.0'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :production do
gem 'rails_12factor'
gem 'pg'
end
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: 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 have followed the steps here >> http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast >> and is working now. Note for beginners, if you are going to use rails and heroku start you application with pg to save time debugging this.
I know it's a very old question but i got the same issue yesterday, am using Ubuntu 16.04 Rails 5.1.6.
I tried every solution that i found on the web but nothing worked for me, so am writing what solved this issue for me so it can help someone who get into this issue.
Simply check what version of Sqlite3 gem you are using:
bundle show sqlite3
for me it was sqlite3-1.4.0
Now just downgrade to a older version of the sqlite3 gem
Simply add gem 'sqlite3', '~> OLDER VERSION' in your gemfile replacing your current version of sqlite3
bundle install
That's it, it should work.
If you are using pg in production than... why sqlite3 confugration here
production:
<<: *default
database: db/production.sqlite3
update it from pg configuration.
Heroku is not compatible with sqlite3, you should use pg
Pg config.
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
I would suggest removing the whole default section of your database.yml and just set everything for the rest:
development:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/development.sqlite3
test:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/test.sqlite3
production:
adapter: postgresql
encoding: unicode
database: <project name>
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>

Rails: Change my development database from sqlite to postgresql

I've got a Rails app which currently uses sqlite in development and postgresql in production on Heroku. However, I'm trying to change this so that I use postgresql in development.
My Gemfile currently looks like this:
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'bcrypt', '3.1.7'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'jquery-ui-rails', '~> 4.2.1'
gem 'turbolinks', '2.3.0'
gem 'jquery-turbolinks'
gem 'jbuilder', '2.2.3'
gem 'sdoc', '0.4.0', group: :doc
gem 'chart-js-rails'
gem 'gon'
gem 'lodash-rails'
group :development, :test do
#gem 'sqlite3', '1.3.9'
gem 'pg', '0.17.1'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '3.1.0'
end
So in the development group I've replaced the 'sqlite3' gem with the 'pg' gem.
I've modified the database.yml to look something like this:
default: &default
adapter: postgresql
host: localhost
pool: 5
timeout: 5000
development:
<<: *default
database: abc_development
username: abc
password: password1
test:
<<: *default
database: abc_test
username: abc
password: password1
production:
<<: *default
database: abc_production
username: abc
password: password1
I've executed bundle install --without production however, when I try bundle exec rake db:create db:migrate I receive the following 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 ActiveRecord).
As far I see Rails is right: you specified the pg gem only in group production, but you said you want to use it also in development.
So just move it from the production group to the beginning of your Gemfile

how to specify the database in the config?

please help solve the problem.
I made a website. I use sqlite3.
database.yml:
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bootstrap-sass', '3.2.0.0'
gem 'bcrypt', '~> 3.1.7'
gem 'populate'
gem 'faker'
gem 'russian', '~> 0.6.0'
gem 'will_paginate'
gem 'paperclip'
gem 'fancybox2-rails', '~> 0.2.8'
gem 'rspec-rails'
gem 'factory_girl'
gem 'database_cleaner'
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
end
on the local server it is running. I uploaded it to heroku. as a result of getting the following error message:
500 internal server error
heroku uses postgresql. that's the problem. please help set up configuration
It is as it says: You need to add pg gem. Include the following line in your Gemfile
gem 'pg', group: :production
And you in your config/database.yml file, you can write the following code with appropriate modifications:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
production:
adapter: postgresql
encoding: unicode
database: myapp_prouduction
pool: 5
username: myapp
password: password1

Could not load 'active_record/connection_adapters/sqlite3_adapter'. Windows 7 local server

I have been going through the Rails tutorial, https://www.railstutorial.org/book/static_pages, and the first two chapters went fine. I installed Rails on my Laptop made a hello world app and a toy with the scaffold generator. There were some things I had to do differently because I am doing everything locally instead of on a cloud environment and because I am using Windows 7, but I got everything to work.
When I tried running a local server and going to my http://localhost:3000/static_pages/home page, i got the error in the Title. The following is the full error message:
Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.
I figure it must be something with the Gemfile because that was the only difference between this and my other apps, I'm just not sure what to change. I included the Gemfile and the database.yml. Thanks in advance.
#Gemfile - sample_app
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'faker', '1.4.2'
gem 'carrierwave', '0.10.0'
gem 'mini_magick', '3.8.0'
gem 'fog', '1.23.0'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '2.11.1'
end
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: 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
In your Gemfile, you specify sqlite3 (the 'sqlite3' gem) for development and testing, and postgres (the 'pg' gem) for production.
But in your database.yml, you've specified the production database as sqlite3:
<database.yml>
...
production:
<<: *default
database: db/production.sqlite3
which will try to use the default adapter (sqlite3) and will look for a database file with .sqlite3 as the file extension.
You need to change your database.yml so that it uses a postgres database:
production:
adapter: postgresql
encoding: unicode
pool: 5
database: db/production
(Make sure you have postgres running on your machine either as an app or as a service.)
Or you can change your Gemfile so that you use SQLite3 for production, too.
If you tried to run the production version on your Windows machine but perhaps were running a development or test version on your OSX machine, this could be the problem.

rake figaro:heroku is returning an error with sqlite3

I am trying to set up my app on Heroku and as I understand it the issue I am having is resulting from trying to run rake figaro:heroku
Every time I run it I get the following error:
rake aborted!
LoadError: cannot load such file -- sqlite3/database
C:/Users/Zac/GitHub/Bitsy/config/application.rb:7:in `<top (required)>'
C:/Users/Zac/GitHub/Bitsy/Rakefile:4:in `<top (required)>'
My gem file is set up as such:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.0.4'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem "paperclip", "~> 3.0"
gem "paperclip-dropbox", ">= 1.1.7"
gem "figaro"
gem 'devise'
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development, :test do
gem 'sqlite3'
end
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
And my database.yml is as:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I have changed the production adapter and database previously and it doesn't seem to make a difference. I have spent about 5 hours searching for help on this to no avail so if anyone could point me in the right direction I would be very appreciative.
you cannot use sqlite3 on Heroku.. you have to use Postgres (you may already know that).
You should try to use the same db in development as production to avoid inconsistencies, so in this case, use PG in every environment.
.. but to fix your problem, try changing the database.yml production entry to:
production:
adapter: postgresql
database: your_app_production
pool: 5
timeout: 5000
that should work

Resources