The PGconn, PGresult, and PGError constants are deprecated on Heroku and Rails - sql

I reset the database on Heroku in the database setting and ran heroku run rake db:migrate. Although it created all the data tables with correct columns per local database, it does not migrate any existing local data. Heroku database is just empty. In the past I ran the above code and worked fine but this time got stuck as shown below:
mac-a:skiny ran$ heroku run bundle exec rake db:migrate
Running bundle exec rake db:migrate on ⬢ blooming-citadel-66205... up, run.3076 (Hobby)
The PGconn, PGresult, and PGError constants are deprecated, and will be
removed as of version 1.0.
You should use PG::Connection, PG::Result, and PG::Error instead, respectively.
Called from /app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
ActiveRecord::SchemaMigration Load (1.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
As a note, I quit mac terminal while running heroku run rake db:reset and maybe there is some internal error...

Try to the following, edit your Gemfile like below
gem 'pg', '~> 0.20.0'
Or
gem 'pg', '~> 0.11'
Then bundle install and try, it's working on my hand.
Note: the tilde sign before the >, that's not a dash
Once you make this update in your group production of your Gemfile, ensure you run bundle install --without production (to update Gemfile.lock file), do a git add/commit cycle, then re-deploy to Heroku.
Hope it helps

Migrations are not supposed to create data. They define tables, indexes, constraints, etc.
You should use db:seed, after migrating, to create data (besides adding pg version as mentioned on other answer.)

Related

no such file to load -- pg unable to run migrations manually

I am using pg for dev and test in local machine only
its in dev and test group out in gemfile
group :development, :test do
gem 'pg'
end
rails version is
Rails 3.0.19
ruby 1.8.7
I used moonshine or shorter cut of capistrano
capistrano:deploy
the migrations file can't be migrated using capistrano, throws an error.
I am trying to run migrations manually inside server
bundle exec rake db:migrate VERSION=20140205173759_add_hebrew_to_piles.rb
I have three files and wanted to run rake migrate on each one (got it from stackoverlow, rails run specific migration)
and got
no such file to load -- pg
I am using mysql as production database so I don't understand why is looking for pg inside my server? any help?

An error occurred while installing pg (0.12.2), and Bundler cannot continue

I'm following the Michael Hartl Ruby on Rails Tutorial & there is a part where he is he he instructs you to update your Gemfile to include:
group :production do
gem 'pg', '0.12.2'
end
And then enter the below commands in your terminal:
bundle update
bundle install --without production
When you run the bundle update command it throws back the below errors.
sample_app:$ bundle update
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.0.3)
Using i18n (0.6.4)
etc
[omitted lines for brevity]
etc
Using railties (3.2.12)
Using coffee-rails (3.2.2)
Installing diff-lcs (1.1.3)
Using jquery-rails (2.0.2)
Installing pg (0.12.2)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/home/ross/.rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
etc
[omitted lines for brevity]
etc
Gem files will remain installed in /home/ross/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.12.2 for inspection.
Results logged to /home/ross/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.12.2/ext/gem_make.out
An error occurred while installing pg (0.12.2), and Bundler cannot
continue.
Make sure that `gem install pg -v '0.12.2'` succeeds before bundling.
sample_app:$
I was able to overcome this error easily be removing the 'pg', '0.12.2' gem from the Gemfile & replacing it after running the bundle update command. This seems to work fine as the 'pg', '0.12.2' gem is aslo omitted in the without production flag in the latter bundle install --without production.
The 'pg', '0.12.2' gem is only needed for deploying to heroku with the correct database & everything works fine even when I deployed it to heroku but I'm just wondering if this is an error in the Tutorial or am I missing something bigger here?
It's also annoying to have to remove this Gem everytime I run bundle update, is bundle update really that necessary?
Thanks in Advance
I'm following the same tutorial and I think it's redundant to run update without modifying existing dependency, but in this case it's even causing problems because update command has not --without argument.
I stumbled upon Rails 3 cheatsheet which mentions bundler workflow this way:
After adding or removing dependencies from Gemfile
$ bundle
Commit Gemfile and Gemfile.lock
After modifying existing dependency versions
$ bundle update
Commit Gemfile and Gemfile.lock
I read man pages for bundle update and tried RECOMMENDED WORKFLOW which consists of running bundle update after bundle install.
In my case (some output omitted):
$ bundle install --without production
Resolving dependencies...
Using rake (10.0.3)
...
Installing rspec-core (2.11.1)
Your bundle is complete!
Gems in the group production were not installed. <-- check
$ bundle update
Resolving dependencies...
Using rake (10.0.3)
...
Using uglifier (1.2.3)
Your bundle is updated!
Gems in the group production were not installed. <-- check
I tried it with new RVM gem set and everything was installed correctly.
After that Gemfile.lock contains pg (0.12.2) and deploying to Heroku works.
RECOMMENDED WORKFLOW
In general, when working with an application managed with bundler, you
should use the following workflow:
After you create your Gemfile for the first time, run
$ bundle install
Check the resulting Gemfile.lock into version control
$ git add Gemfile.lock
When checking out this repository on another development machine, run
$ bundle install
When checking out this repository on a deployment machine, run
$ bundle install --deployment
After changing the Gemfile to reflect a new or update dependency,
run
$ bundle install
Make sure to check the updated Gemfile.lock into version control
$ git add Gemfile.lock
If bundle install reports a conflict, manually update the specific
gems that you changed in the Gemfile
$ bundle update rails thin
If you want to update all the gems to the latest possible versions
that still match the gems listed in the Gemfile, run
$ bundle update
Installation of postgres fails with this error : "Can't find the 'libpq-fe.h header"
Looks like a lot of people faced this problem, good news is : stackoverflow has the answer ;)
Can't find the 'libpq-fe.h header when trying to install pg gem
(or at least it should help you to look in the right direction)

Heroku rake db:migrate doesn't work

Ruby 1.9.2 Rails 3.1
After the day of fighting with heroku issues I was able to finally have the command 'git push heroku master to work properly'.
Gemfile has proper enties and pg gem is installed
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
At this point application has been uploaded and start page can be viewed here http://growing-day-7939.heroku.com/
The next step should be migrating the database to the heroku with 'heroku rake db:migrate"
and it always fails.
c:\RailsInstaller\work\rfclub>heroku rake db:migrate
No app specified.
Run this command from an app folder or specify which app to use with --app <app name>
Other heroku commands fail with the same error as above - heroku ps, heroku logs, heroku config.
I've tried deleting and recreating the app, even tried creating the new heroku account and repeating the steps, but nothing seems to help.
Any ideas would be appreciated, I'm really stuck.
I think you'll need to deploy Rails 3.1 apps on Heroku's Cedar stack, via heroku create --stack cedar. The migration command will be heroku run rake db:migrate.

Heroku error when launch rails3.1 app missing postgres gem

I am trying to deploy to heroku.
Rails 3.1.0.rc4,
I get the following error from Heroku logs:
Starting process with command: `thin -p 48902 -e production -R /home/heroku_rack/heroku.ru start`
2011-06-20T11:25:44+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `rescue in establish_connection': Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.) (RuntimeError)
I tried to install the activerecord-postgresql-adapter but then I get this error:
Could not find gem 'activerecord-postgresql-adapter (>= 0)' in any of the gem sources listed in your Gemfile.
So I tried to add this to my gem file
gem 'pg'
which produced this error:
Installing pg (0.11.0) with native extensions /Users/imac/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:533:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
any ideas?
You don't have to install Postgres locally. In your Gemfile, put 'pg' in group :production, as johnny-grass suggests, and then when you run bundle, just specify --without production, like this:
bundle --without production
Unfortunately, you have to remember this argument when you run bundler, but at least you don't have to install and maintain postgres locally.
Please note that Heroku "strongly recommends against" using sqlite, saying that "Your production and development environment should be as close to identical as possible" http://devcenter.heroku.com/articles/rails3
Do you have PostgreSQL installed on your computer? If you don't then install it first, then install the pg gem.
# gemfile
group :production do
gem 'therubyracer-heroku', '0.8.1.pre3' # you will need this too
gem 'pg'
end
I found a solution in this Heroku article.
As Jared said, they suggest to create a different group for postgresql.

How to empty a Heroku database

I'm working on a Ruby on Rails 3 webapp on Heroku. How do I empty the database?
To drop the database, if you are using SHARED_DATABASE_URL:
$ heroku pg:reset DATABASE_URL
Now to recreate the database with nothing in it:
$ heroku run rake db:migrate
To populate the database with your seed data:
$ heroku run rake db:seed
---OR---
You can combine the last two (migrate & seed) into one action by executing this:
$ heroku run rake db:setup
Edit 2014-04-18: rake db:setup doesn't work with Rails 4, it fails with a Couldn't create database error.
Edit 2014-10-09: You can use rake db:setup with Rails 4. It does give you a Couldn't create database error (because the database was already created using the heroku pg:reset command). But it also loads your database schema and your seeds after the error message.
You can do this with pretty much any rake command, but there are exceptions. For example, db:reset doesn't work via heroku run rake. You have to use pg:reset instead.
More information can be found in Heroku's documentation:
Running Rake Commands
Reset Postgres DB
Heroku has deprecated the --db option now, so now use:
heroku pg:reset DATABASE_URL --confirm {the name of your app}
It's a little confusing because you use the literal text SHARED_DATABASE but where I have written {the name of your app} substitute the name of your app. For example, if your app is called my_great_app then you use:
heroku pg:reset DATABASE_URL --confirm my_great_app
To drop the database:
$ heroku pg:reset SHARED_DATABASE --confirm NAME_OF_THE_APP
To recreate the database:
$ heroku run rake db:migrate
To seed the database:
$ heroku run rake db:seed
**Final step
$ heroku restart
The current, ie. 2017 way to do this is:
heroku pg:reset DATABASE
https://devcenter.heroku.com/articles/heroku-postgresql#pg-reset
Now the command is
heroku pg:reset DATABASE_URL --confirm your_app_name
this way you can specify which app's db you want to reset.
Then you can run
heroku run rake db:migrate
heroku run rake db:seed
or direct for both above commands
heroku run rake db:setup
And now final step to restart your app
heroku restart
I contacted Heroku support, and they confirmed that it is a bug with the latest gem (I am using heroku-2.26.2)
Charlie - we are aware of this issue with the 'heroku' gem and are
working to fix it.
Here's the issue if you care to follow-along -
https://github.com/heroku/heroku/issues/356
Downgrading to an earlier version of the 'heroku' gem should help. I've been using v2.25.0 for most of today without issue.
Downgrade with the following commands:
gem uninstall heroku
gem install heroku --version 2.25.0
If you already have multiple gems installed, you may be presented with:
Select gem to uninstall:
1. heroku-2.25.0
2. heroku-2.26.2
3. All versions
Just uninstall #2 and rerun the command. Joy!
The complete answer is (for users with multi-db):
heroku pg:info - which outputs
=== HEROKU_POSTGRESQL_RED <-- this is DB
Plan Basic
Status available
heroku pg:reset HEROKU_POSTGRESQL_RED --confirm app_name
More information found in: https://devcenter.heroku.com/articles/heroku-postgresql
Now it's diffrent with heroku. Try:
heroku pg:reset DATABASE --confirm
Now it's also possible to reset the database through their web interface.
Go to dashboard.heroku.com select your app and then you'll find the database under the add-ons category, click on it and then you can reset the database.
Today the command
heroku pg:reset --db SHARED_DATABASE_URL
not working for shared plans, I'm resolve using
heroku pg:reset SHARED_DATABASE
Check your heroku version. I just updated mine to 2.29.0, as follows:
heroku --version
#=> heroku-gem/2.29.0 (x86_64-linux) ruby/1.9.3
Now you can run:
heroku pg:reset DATABASE --confirm YOUR_APP_NAME
Then create your database and seed it in a single command:
heroku run rake db:setup
Now restart and try your app:
heroku restart
heroku open
Login to your DB using
heroku pg:psql and type the following commands:
drop schema public cascade;
create schema public;
In case you prefer to use Heroku Web-site:
Go to https://postgres.heroku.com/databases
Select the database you want to reset
Click on a settings button in the right upper corner
Click "Reset Database" as shown below:
type in "RESET" and press ok
This is what worked for me.
1.clear db.
heroku pg:reset --app YOUR_APP
After running that you will have to type in your app name again to confirm.
2.migrate db to recreate.
heroku run rake db:migrate --app YOUR_APP
3.add seed data to db.
heroku run rake db:seed --app YOUR_APP
Assuming you want to reset your PostgreSQL database and set it back up, use:
heroku apps
to list your applications on Heroku. Find the name of your current application (application_name). Then run
heroku config | grep POSTGRESQL
to get the name of your databases. An example could be
HEROKU_POSTGRESQL_WHITE_URL
Finally, given application_name and database_url, you should run
heroku pg:reset `database_url` --confirm `application_name`
heroku run rake db:migrate
heroku restart
If you are logged in from the console, this will do the job in the latest heroku toolbelt,
heroku pg:reset --confirm database-name
I always do this with the one-liner 'heroku pg:reset DATABASE'.
Best solution for you issue will be
heroku pg:reset -r heroku --confirm your_heroku_app_name
--confirm your_heroku_app_name
is not required, but terminal always ask me do that command.
After that command you will be have pure db, without structure and stuff, after that you can run
heroku run rake db:schema:load -r heroku
or
heroku run rake db:migrate -r heroku