postgresql - Ruby Sinatra/Postgres - can't connect to heroku database with PG.connect? -
i trying site set on heroku using sinatra , postgresql. worked locally (connecting local database), after pushing heroku , changing pg.connect
reflect that, internal server error moment page tries access database.
require 'uri' require 'pg' uri = uri.parse(env['database_url']) def db(uri) begin connection = pg.connect(uri.hostname, uri.port, nil, nil, uri.path[1..-1], uri.user, uri.password) yield(connection) ensure connection.close end end
i pretty sure these parsing correctly, because env['database_url']
displays full postgres://user:password@host:port/database
information i'm expecting, , if same in irb uri.hostname, ui.port, etc return what's expected .
this first time trying site working on heroku, not sure how troubleshoot this. (and googled of yesterday.)
results heroku pg
:
=== database_url plan: hobby-dev status: available connections: 0/20 pg version: 9.4.2 created: 2015-05-30 19:24 utc data size: 17.7 mb tables: 5 rows: 9320/10000 (in compliance, close row limit) fork/follow: unsupported rollback: unsupported
and tables show when when heroku pg:psql <database>
cli.
some answers i've seen said add database.yml
root app directory, so:
production: adapter: 'postgresql' database: '<database>' host: env['database_url'] username: '<username>'
there's simple i'm missing, haven't seen complete guide sinatra/psql on heroku - nothing goes setting , connecting database. (everything seems rails-related.)
in database.yml
file need specify correct host host
entry. passing stored in database_url
(something postgres://user:password@host:port/database
) should host.
you need specify port if isn't default postgresql.
edit: should point out if plan store host (or else - should username , password) in environment variable you'll need wrap it, e.g. <%= env['host'] %>
, not env['host']
(i.e. how have in database.yml excerpt above)
Comments
Post a Comment