如何使用url连接到postgresql

Omn*_*ent 34 postgresql ruby-on-rails heroku taps

我曾问一个较早的问题,其并未得到任何回复.

基本上我invalid database url在尝试时会出错heroku db:push.

我想我可以尝试显式提供数据库URL.

我试过了:

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

但这给了错误:

Failed to connect to database:
  Sequel::DatabaseConnectionError -> PGError fe_sendauth: no password supplied
Run Code Online (Sandbox Code Playgroud)

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

Håv*_*d S 54

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

  • 只需删除密码,即`db push postgres:// username @ localhost/myrailsdb` (4认同)
  • 如果您的本地数据库用户没有密码怎么办? (2认同)
  • 如果未提供密码,则会出现无法连接到数据库的错误:Sequel::DatabaseConnectionError -> PGError fe_sendauth:未提供密码 (2认同)

ma1*_*w28 11

以下是如何在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
Run Code Online (Sandbox Code Playgroud)

  • 您的进口/要求是什么? (2认同)

Tou*_*umi 9

根据文件

postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
Run Code Online (Sandbox Code Playgroud)

例子

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
Run Code Online (Sandbox Code Playgroud)


use*_*677 5

编辑postgresql配置(pg_hba.conf)文件并将"host"类型方法更改为"trust".但请注意,这不安全.

# 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
Run Code Online (Sandbox Code Playgroud)

重新启动postgresql服务器并重新运行该命令

$ heroku db:push postgres://postgres@localhost/myrailsdb
Run Code Online (Sandbox Code Playgroud)

以下是我的回答:https://stackoverflow.com/a/7667344/181677