简体   繁体   English

如何使用URL连接到PostgreSQL

[英]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 . 基本上,当我尝试执行heroku db:push时,我得到一个错误的invalid database url

I figured I can try explicitly providing the database url. 我认为我可以尝试显式提供数据库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? 提供用户名和密码的格式是什么?

尝试heroku db:push postgres://username:password@localhost/myrailsdb

Here's how to do it in a Ruby script: 这是在Ruby脚本中执行此操作的方法:

# 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

Edit your postgresql configuration (pg_hba.conf) file and change 'host' type method to 'trust'. 编辑您的postgresql配置(pg_hba.conf)文件,并将“主机”类型方法更改为“信任”。 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 重新启动您的postgresql服务器并重新运行命令

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

Here is the reference to my answer: https://stackoverflow.com/a/7667344/181677 这是对我的答案的参考: https : //stackoverflow.com/a/7667344/181677

Heroku cli更改了命令。

heroku pg:push postgres://username:password@localhost/myrailsdb

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM