How to connect to postgresql using url

2020-02-20 06: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.

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?

5条回答
Luminary・发光体
2楼-- · 2020-02-20 07:11

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
查看更多
Explosion°爆炸
3楼-- · 2020-02-20 07:23

Try heroku db:push postgres://username:password@localhost/myrailsdb.

查看更多
ら.Afraid
4楼-- · 2020-02-20 07:30

according to documentation

postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

examples

postgresql://
postgresql://localhost
postgresql://localhost:5432
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://localhost/mydb?user=other&password=secret
查看更多
▲ chillily
5楼-- · 2020-02-20 07:33

Edit your postgresql configuration (pg_hba.conf) file and change 'host' type method to 'trust'. But be aware that this is not secure.

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 md5

Restart your postgresql server and re-run the command

$ heroku db:push postgres://postgres@localhost/myrailsdb

Here is the reference to my answer: https://stackoverflow.com/a/7667344/181677

查看更多
Bombasti
6楼-- · 2020-02-20 07:34

Heroku cli has changed the command.

heroku pg:push postgres://username:password@localhost/myrailsdb
查看更多
登录 后发表回答