【问题标题】:How to use connection string with sequel for postgresql如何将连接字符串与 sequel 一起用于 postgresql
【发布时间】:2019-04-22 05:08:16
【问题描述】:

我想使用以下形式的连接字符串连接到 postgresql 数据库

host=localhost user=x password=y dbname=z

这在传递给 psql 时工作正常

$ psql "host=localhost user=x password=y dbname=z"

当传递给PG gem时

> PG.connect("host=localhost user=x password=y dbname=z")

但是当我尝试将它传递给续集时它失败了

> Sequel.postgres(constr)
Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory

如何使 sequel 连接到我的数据库?

【问题讨论】:

  • 你的应用是 Rails 应用吗?
  • 花见和葡萄的组合如果有帮助的话

标签: ruby postgresql sequel


【解决方案1】:

您可以使用带有连接信息的字符串,例如:

conn_str = 'postgres://localhost/database_name?user=user&password=password'


DB = Sequel.connect(conn_str)

使用哈希检查how to open connections 的 Sequel 文档

DB = Sequel.connect(
  adapter: 'postgres',
  host: 'localhost',
  database: 'database_name',
  user: 'database_user',
  password: 'password'
)

那么你应该可以运行了:

DB[:table_name].where(:attribute => 5000..10000)

或者你可以直接使用 postgres 方法:

DB = Sequel.postgres('my_db', :user => 'user', :password => 'password', :host => 'localhost')

检查devhints打开连接快捷方式

【讨论】:

  • 是的,我知道这种方式,但是我从环境中得到了我的问题中格式的连接字符串,并希望避免解析它:/
猜你喜欢
  • 2020-10-05
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
相关资源
最近更新 更多