【问题标题】:How to connect to postgresql using url如何使用 url 连接到 postgresql
【发布时间】:2011-01-11 05:24:27
【问题描述】:

我之前问过question,但没有得到任何回复。

当我尝试执行heroku db:push 时,基本上会收到错误invalid database url

我想我可以尝试显式提供数据库 url。

我试过了:

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

但这给出了错误:

Failed to connect to database:
  Sequel::DatabaseConnectionError -> PGError fe_sendauth: no password supplied

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

【问题讨论】:

    标签: ruby-on-rails postgresql heroku taps


    【解决方案1】:

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

    【讨论】:

    • 本地数据库用户没有密码怎么办?
    • 只需输入密码,即db push postgres://username@localhost/myrailsdb
    • 如果没有提供密码,这将给出连接数据库失败的错误:Sequel::DatabaseConnectionError -> PGError fe_sendauth: no password provided
    • 您还可以在末尾添加查询参数以指定其他属性 - 例如?pool=5。对于 Ruby on Rails,DATABASE_URL 中指定的任何属性都将覆盖config/database.yml 中指定的属性。见:edgeguides.rubyonrails.org/…
    【解决方案2】:

    编辑您的 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
    

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

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

    这是我的回答的参考:https://stackoverflow.com/a/7667344/181677

    【讨论】:

    • pg_hba.conf 文件在哪里?
    • 其实,找到了。对于 ubuntu、Debian/Ubuntu:/etc/postgresql/9.0/main/pg_hba.conf
    • 如果我在远程服务器 (heroku) 上遇到有关 pg_hba.conf 的错误怎么办?我不能在那里改变任何东西,改变本地文件没有任何区别,ofc
    【解决方案3】:

    以下是如何在 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
    

    【讨论】:

    • 您的进口/要求是什么?
    【解决方案4】:

    Heroku cli 已更改命令。

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

    【讨论】:

      【解决方案5】:

      根据文档

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

      例子

      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
      

      【讨论】:

      • "postgresql://localhost/mydb?user=other&password=secret" 似乎没有使用指定的密码。只有“postgresql://user:secret@localhost”有效。
      猜你喜欢
      • 2018-12-01
      • 2023-04-06
      • 2016-12-24
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多