Creating postgres database on Heroku with Rails 4 error - ruby-on-rails

PG::UndefinedTable: ERROR
I am trying to deploy a Rails 4 app on Heroku with a postgres database. I have precompiled assets, but now when I run:
heroku run rake db:migrate
I get this error:
PG::UndefinedTable: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD COLUMN "invited_by_user_id" integer
rake aborted!
Commonly suggested solutions on Stack Overflow:
I have tried resetting the database with:
heroku pg:reset
The database resets, but I still can't run db:migrate.
I have also tried:
heroku run db:setup
but I get an error that says:
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
I was using Rails 4.1.8, and saw this question which suggested maybe I needed to update to rails 4.1.9 or above, but even now that I'm using Rails 4.2.2, no luck.
Additional possibly relevant info:
Connecting to the postgres database on heroku with
heroku pg:psql
then listing the databases with '\l' gives me a long list of databases with cryptic names:
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------+----------------+----------+-------------+-------------+-----------------------------------
d100o5a2gii5he | rdxtahlqikernc | UTF8 | en_US.UTF-8 | en_US.UTF-8 | rdxtahlqikernc=CTc/rdxtahlqikernc
d102ehvv3umqam | ibuffadisyolrb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | ibuffadisyolrb=CTc/ibuffadisyolrb
d103n2kea6j5sa | tzzkfcinullytw | UTF8 | en_US.UTF-8 | en_US.UTF-8 | tzzkfcinullytw=CTc/tzzkfcinullytw
d10597iiu347nk | eltovdzaxajtiu | UTF8 | en_US.UTF-8 | en_US.UTF-8 | eltovdzaxajtiu=CTc/eltovdzaxajtiu
d108g0imdgn865 | pvzysfivyhcnaa | UTF8 | en_US.UTF-8 | en_US.UTF-8 | pvzysfivyhcnaa=CTc/pvzysfivyhcnaa
...
dvs2j8tkv153m | zjefhzzizozpwy | UTF8 | en_US.UTF-8 | en_US.UTF-8 | zjefhzzizozpwy=CTc/zjefhzzizozpwy
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres
(4787 rows)
I'm not sure exactly how to interpret that.
I am relatively new new to Rails, but have successfully setup several databases on Heroku in the past. Following the Heroku documentation hasn't helped, and I'm not sure where to go from here.
How can I get db:migrate to complete without errors?
NOTE: I solved this just after posting. See answer below.

Minutes after posting this question I figured it out, and I'm hoping this might help someone else out.
The solution was to upgrade from Rails 4.1.8 to Rails 4.2.2.
Then run
heroku run rake db:schema:load
before
heroku run rake db:migrate
For some reason that wouldn't run for my project with Rails 4.1.8. So, after upgrading to Rails 4.2.2, I ran the schema load and migrate commands with no errors.

Related

heroku run rails db:seed failed - Rails 7 + fixtures

I have successfully deployed my Rails app with Heroku, heroku run rails db:migrate, although heroku run rails db:seed failed.
Here is the error message
➜ quote-editor git:(master) heroku run rails db:seed
Running rails db:seed on ⬢ awesome-quote-editor... up, run.6832 (Free)
== Seeding the database with fixtures ==
WARNING: Rails was not able to disable referential integrity.
This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.
cause: PG::InsufficientPrivilege: ERROR: permission denied: "RI_ConstraintTrigger_c_783704777" is a system trigger
rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618"
DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
My setup
Ubuntu 20.04.2
Ruby 3.1.2
Rails 7.0.4
pg 1.4.3 with native extensions
I have both tried to give superuser privileges
ALTER USER myuser WITH SUPERUSER;
and
GRANT ALL PRIVILEGES ON DATABASE quote_editor_production TO myuser;
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_development | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_production | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/paulinetw
quote_editor_test | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
database.yml
default: &default
adapter: postgresql
encoding: unicode
development:
<<: *default
database: quote_editor_development
test:
<<: *default
database: quote_editor_test
production:
<<: *default
database: quote_editor_production
username: quote_editor
password: <%= ENV["QUOTE_EDITOR_DATABASE_PASSWORD"] %>
I have tried a lot of suggestions but still can't find a way out. I will really appreciate if somebody could help.
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618" DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
Your seed file is trying to use a Quote with ID 309456473, but that quote doesn't exist.
UPDATE:
Here are my assumptions:
In producution, you run heroku run rails db:migrate, which works
In production, you run heroku run rails db:seed, which fails.
In your seed file you have some command that relies upon some code similar to Quote.find(309456473) and this fails in production
In your seed file, when you create Quotes, you are manually setting each quote's ID as you create it.
Setting the ID of your records on creation is not a good idea. Let the Db generate keys.
In your seed file, wherever you are looking up records by ID, find a different method to locate the record you want by some other attribute.
Don't assume that Heroku Postgres respects or preserves your record IDs from the seed file.

Unnecessary files are generated when rails application is launched

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
This began to occur after tweaking the settings around --binstubs.
The following are generated files.
bin/brakeman
bin/bundle-audit
bin/bundler-audit
bin/byebug
bin/cap
bin/capify
bin/chromedriver-helper
bin/chromedriver-update
bin/coderay
bin/console
bin/dotenv
bin/fission
bin/fog
bin/foreman
bin/generate-api
bin/haml
bin/htmldiff
bin/httpclient
bin/ldiff
bin/listen
bin/maruku
bin/marutex
bin/nokogiri
bin/pry
bin/rackup
bin/rbvmomish
bin/restclient
bin/rspec
bin/rubocop
bin/ruby-parse
bin/ruby-rewrite
bin/sass
bin/sass-convert
bin/scss
bin/sprockets
bin/thor
bin/tilt
bin/unicorn
bin/unicorn_rails
bin/whenever
bin/wheneverize
The following is the message on the terminal.
20:39:07 web.1 | Beginning in Rails 4, Rails ships with a `rails` binstub at ./bin/rails that
20:39:07 web.1 | should be used instead of the Bundler-generated `rails` binstub.
20:39:07 web.1 |
20:39:07 web.1 | If you are seeing this message, your binstub at ./bin/rails was generated by
20:39:07 web.1 | Bundler instead of Rails.
20:39:07 web.1 |
20:39:07 web.1 | You might need to regenerate your `rails` binstub locally and add it to source
20:39:07 web.1 | control:
20:39:07 web.1 |
20:39:07 web.1 | rails app:update:bin # Bear in mind this generates other binstubs
20:39:07 web.1 | # too that you may or may not want (like yarn)
20:39:07 web.1 |
20:39:07 web.1 | If you already have Rails binstubs in source control, you might be
20:39:07 web.1 | inadverently overwriting them during deployment by using bundle install
20:39:07 web.1 | with the --binstubs option.
20:39:07 web.1 |
20:39:07 web.1 | If your application was created prior to Rails 4, here's how to upgrade:
20:39:07 web.1 |
20:39:07 web.1 | bundle config --delete bin # Turn off Bundler's stub generator
20:39:07 web.1 | rails app:update:bin # Use the new Rails executables
20:39:07 web.1 | git add bin # Add bin/ to source control
20:39:07 web.1 |
20:39:07 web.1 | You may need to remove bin/ from your .gitignore as well.
20:39:07 web.1 |
20:39:07 web.1 | When you install a gem whose executable you want to use in your app,
20:39:07 web.1 | generate it and add it to source control:
20:39:07 web.1 |
20:39:07 web.1 | bundle binstubs some-gem-name
20:39:07 web.1 | git add bin/new-executable
20:39:07 web.1 |
Which setting should I change to fix it?

Where is my Rails database?

I paid someone to create a program with Ruby on Rails where it scrapes data and puts it in a Postgres database. In the program's directory are the standard Rails folders, application, "bin", "config" and other directories.
I'm trying to see a list of the columns in a table. I think the best, or only way, to do this is to log into the actual database, and print it out. I'm trying to use a "psql" command to log in but it is saying:
psql: FATAL: database "dan" does not exist
I'm not sure where the database is, or how I can find it.
This is what the config/database.yml contains:
development:
adapter: postgresql
database: danwork
pool: 5
timeout: 5000
encoding: unicode
username: dan
password: supersecretpassword
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: sports
pool: 5
timeout: 5000
encoding: unicode
username: namename
password: sports_db
Where is my database?
How could I find the database on my own using some linux commands, like find . -iname '...'?
How do I log in, and print out all the columns for the table named "games"?
You want rails dbconsole or rails db.
From the Rails Guide:
rails dbconsole figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
You can also use the alias “db” to invoke the dbconsole: rails db.
To see the contents of your games table:
~/Documents/workspace/<project-dir>$ rails db
psql (9.6.3)
Type "help" for help.
development=# select * from games;
id | date | description | city | state
----+------+-------------+----------------------+-------
1 | | | Boston | MA
2 | | | Seattle | WA
(2 rows)
development=# \q
~/Documents/workspace/<project-dir>$
If you really want to use psql -U <username> <dbname>, you can find these parameters as described in this answer and paraphrased here:
~/Documents/workspace/<project-dir>$ rails c
Loading development environment (Rails 4.1.4)
2.3.1 :001 > Rails.configuration.database_configuration[Rails.env]
=> {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"development"}
2.3.1 :002 > exit
~/Documents/workspace/<project-dir>$ psql development
psql (9.6.3)
Type "help" for help.
development=# \q
~/Documents/workspace/<project-dir>$
As you can see, if no username is returned, you won't need the -U flag.
Your database is running on a server independent of Rails (it will be set up by your coder). It looks like your database is on your localhost.
It will be dependent on your server OS.
If you're looking to show column names in your database, you'll be able to do this in the Rails Console:
$ rails c
$ Game.column_names
To access your database using plain PostgreSQL
su username
psql
\db database_name;
To connect with psql, you must specify the database name.
If you're using password auth, simply psql -U username dbname.
If you're using peer auth, sudo -u username psql dbname.
In your case you seem to be using password auth and connecting to the local host, so I suggest:
psql -U dan danwork
and entering the password when prompted. (You can use a ~/.pgpass file to save the password if desired; see the pgpass documentation)

Postgres drop database in rails app with rake command

I am having trouble dropping my postgres database for a rails app - switched recently from sqlite after deploying to heroku. When running my rake task to aggregate data and populate the database, rake db:create db:migrate all works, but db:drop is not working. I am getting the following error in my terminal after running rake db:drop db:migrate db:create all. 'all' is a rake task for an aggregator.
Couldn't drop something_development : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
>
something_development already exists
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
'This is a look at my database.yml file...`
development:
adapter: postgresql
host: localhost
database: something_development
username: username
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: username
database: something_production
Also here is a list of databases from psql command line:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+-----------------+----------+---------+-------+-------------------------------------
username | username | UTF8 | en_US | en_US |
something_development | username | UTF8 | en_US | en_US |
template0 | username | UTF8 | en_US | en_US | =c/username+ | | | | | username=CTc/username
template1 | username | UTF8 | en_US | en_US | =c/username + | | | | | username=CTc/username
(4 rows)
EDIT - I am trying to do this in development before pushing to heroku. Do i need to add access privileges to my username? I tried GRANT UPDATE ON something_development TO username but received ERROR: relation "something_development" does not exist. Will remember that for Heroku - thanks.
Does anyone have any ideas on what might be happening or some resources that might point me in the right direction? Appreciate any help - thanks.
At first, you should make sure that which kind of postgres DB using on Heroku: Shared DB or Heroku Postgres.
If you are using Shared DB (it's recommended that shouldn't use any more), unfortunately you have to remove your Heroku app and re-create new one with the same app name.
If you are using Heroku PG, you could find your DB here: https://postgres.heroku.com/databases and just destroy and add another DB, it's like drop and re-create your DB, after that you can run migration as well. With Heroku PG, I'm not sure whether we can run db:drop now (It's been a while since I was fail to use this command on Heroku)
About configuration you should follow few steps below:
install gem pg
using pgAdmin III or something like that to try to connect to your PG server. If you're successful, use this information for your database.yml file.
Notice: When you push your code to Heroku, Heroku will automatically create new database.yml basing on their PG server information, so you don't need to care about configuration database on Heroku. Remember removing sqlite gem before pushing your app to Heroku
On heroku you don't have privileges for drop database, you have to do
heroku pg:reset
If your database is not on heroku, check the user (of postrgesql) has privileges.
Figured out the problem - turns out you cannot rename the database "postgres" because it needs that to manage the database. I had tried renaming postgres to something_development. To fix this, I created a postgres database.

rake db:test:prepare not creating all tables

NOTE: Using Rails 3.0.7, Postgresql 8.4.4-1, rake 0.8.7.
Trying to get rails testing working.
The command rake db:test:prepare appears to work fine --
$ rake db:test:prepare -t
(in /home/beau/looked)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
NOTICE: CREATE TABLE will create implicit sequence "slugs_id_seq" for serial column "slugs.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "slugs_pkey" for table "slugs"
BUT some of the tables are not being created.
These are the "development" tables:
$ psql -d looked -U admin -c '\d'
List of relations
Schema | Name | Type | Owner
--------+-------------------+----------+-------
public | businesses | table | admin
public | businesses_id_seq | sequence | admin
public | categories | table | admin
public | categories_id_seq | sequence | admin
public | schema_migrations | table | admin
public | slugs | table | admin
public | slugs_id_seq | sequence | admin
(7 rows)
Tables created by rake:db:prepare for the test environment --
$ psql -d looked_test -U admin -c '\d'
List of relations
Schema | Name | Type | Owner
--------+-------------------+----------+-------
public | categories | table | admin
public | schema_migrations | table | admin
public | slugs | table | admin
public | slugs_id_seq | sequence | admin
(4 rows)
As you can see it has created some tables, but not businesses, businesses_id_seq or categories_id_seq.
I'm at a loss as to why, can someone help me?
First make sure to run rake db:migrate before rake db:test:prepare.
If that doesn't work, back up your schema.rb somewhere, remove it, then run rake db:schema:dump before running rake db:test:prepare. That will make sure your schema.rb file fully reflects your database.

Resources