postgresql 如何找到本地 postgres 数据库的 URL 路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10632801/
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-10-20 23:55:27  来源:igfitidea点击:

How to find the URL path to a local postgres database?

ruby-on-railspostgresql

提问by Andy Harvey

I have a Rails app running on a postgres database. I'm setting up a background task queue running on the database, and I need to specify the database URL.

我有一个在 postgres 数据库上运行的 Rails 应用程序。我正在设置一个在数据库上运行的后台任务队列,我需要指定数据库 URL。

The various permutations I've tried and would expect all return FATAL: database "database-name" does not exist.

我尝试过的各种排列并希望全部返回FATAL: database "database-name" does not exist

Is there a command that will print this URL?

是否有打印此 URL 的命令?

Or, what should the URL look like if my database.yml looks like this

或者,如果我的 database.yml 看起来像这样,URL 应该是什么样的

development:
    adapter: postgresql
    encoding: unicode
    database: db/database-name

Thanks for suggestions.

感谢您的建议。

采纳答案by Michael Slade

The format appears to be

格式似乎是

postgres://username:password@host/database

You have a /in your database name and (apparently) postgres will accept /in database names, so you need to use either

/的数据库名称中有一个,并且(显然)postgres 将接受/数据库名称,因此您需要使用

postgres:///db/database-name

or

或者

postgres:///db%2Fdatabase-name

Feel free to take out the db/part of your database name - it's only warranted for databases like SQLite that store the db in a local file and need a filename.

随意删除db/数据库名称的一部分 - 它仅适用于 SQLite 等将数据库存储在本地文件中并需要文件名的数据库。

回答by Michael Slade

Not sure how you're attempting to access the database, but if you are attempting to create a script that uses the same database as rails but is run separately, consider using a script that runs in rails. In unix/linux, if you add this line:

不确定您尝试如何访问数据库,但如果您尝试创建一个使用与 rails 相同的数据库但单独运行的脚本,请考虑使用在 rails 中运行的脚本。在 unix/linux 中,如果添加这一行:

#! script/rails runner

to the first line of your script and then make it executable with:

到脚本的第一行,然后使用以下命令使其可执行:

chmod +x myscript

then you can simply run it with

然后你可以简单地运行它

./myscript

and it can use your model classes just like your controller and view code in rails. Then there is no need to access the database manually.

它可以像使用控制器一样使用您的模型类,并在 Rails 中查看代码。这样就不需要手动访问数据库了。