How to connect to postgresql using url

前端 未结 5 1828
予麋鹿
予麋鹿 2020-12-28 12:35

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

5条回答
  •  生来不讨喜
    2020-12-28 13:19

    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
    

提交回复
热议问题