How to connect to postgresql using url
I had asked an earlier question which did not get any replies.
Basically I get an error invalid database url
when I try to do heroku db:push
.
I figured I can try explicitly providing the database url.
I tried:
heroku db:push postgres://postgres@localhost/myrailsdb
But that gave error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PGError fe_sendauth: no password supplied
What is the format for providing username and password?
Solution 1:
Try heroku db:push postgres://username:password@localhost/myrailsdb
.
Solution 2:
Here's how to do it in a Ruby script:
# Connect to database.
uri = URI.parse(ENV['DATABASE_URL'])
postgres = PG.connect(uri.hostname, uri.port, nil, nil, uri.path[1..-1], uri.user, uri.password)
# List all tables.
tables = postgres.exec('SELECT * FROM pg_catalog.pg_tables')
tables.num_tuples.times do |i|
p tables[i]
end