Ruby-on-rails 如何使用 url 连接到 postgresql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2168443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:13:39  来源:igfitidea点击:

How to connect to postgresql using url

ruby-on-railspostgresqlherokutaps

提问by Omnipresent

I had asked an earlier questionwhich did not get any replies.

我之前问过一个问题,但没有得到任何答复。

Basically I get an error invalid database urlwhen I try to do heroku db:push.

基本上,invalid database url当我尝试执行heroku db:push.

I figured I can try explicitly providing the database url.

我想我可以尝试明确提供数据库 url。

I tried:

我试过:

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

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?

提供用户名和密码的格式是什么?

回答by H?vard S

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

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

回答by ma11hew28

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

回答by user181677

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

编辑您的 postgresql 配置 (pg_hba.conf) 文件并将“主机”类型方法更改为“信任”。但请注意,这并不安全。

# 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

回答by Toumi

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

回答by Harry Moreno

Heroku cli has changed the command.

Heroku cli 更改了命令。

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