scramble cadenza

技術ネタのガラクタ置き場

heroku で Postgres 9.4 を使う

イントロ

タイトルの通り。jsonb 型のカラムを取り扱おうとしたら、激しく怒られた。

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
...
PG::UndefinedObject: ERROR:  type "jsonb" does not exist
LINE 1: SELECT 'jsonb'::regtype::oid
...
: SELECT 'jsonb'::regtype::oid/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
...

原因

Postgres の default version が 9.3 だから。jsonb 型は 9.4 からサポートされているので、そんな型しらねーよと言われている。

https://devcenter.heroku.com/articles/heroku-postgresql#version-support-and-legacy-infrastructure

解決法

https://blog.heroku.com/archives/2015/1/8/postgres-9-4-on-heroku

9.4 に upgrade して、DATABASE_URL を切り替えるだけでよい。

% heroku addons:add heroku-postgresql --version=9.4 --app test
Adding heroku-postgresql on test... done, v14 (free)
Attached as HEROKU_POSTGRESQL_YELLOW_URL
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql` to view documentation.

config:unset

% heroku config:unset DATABASE_URL --app test
Unsetting DATABASE_URL and restarting test... done, v15

config:set

% heroku config --app test
=== test Config Vars
....
HEROKU_POSTGRESQL_ROSE_URL:   postgres://xxxxx
HEROKU_POSTGRESQL_YELLOW_URL: postgres://yyyyy
....

upgrade した時のログにも書いてあった通りで、HEROKU_POSTGRESQL_YELLOW_URL という環境変数が attach されているので、この value を DATABASE_URL に set する。

heroku config:set DATABASE_URL=postgres://yyyy

再起動

migration して再起動して反映させる。

heroku run rake db:migrate --app test
heroku run rake db:seed --app test
heroku restart --app test