簡體   English   中英

如何從ruby寫入heroku上的pg數據庫? (不是Rails)

[英]How do I write to a pg database on heroku from ruby? (Not Rails)

我有一個非常簡單的ruby文件,可以在開發環境中的本地主機上正常運行,但是無法連接到Heroku上的postgresql數據庫。

require 'pg'
conn = PG.connect(dbname: 'postgres://uullzzubseknqp:fe8af8be794708c6559d1b521aa285a9500c8645d543fdf23f83bbe7dd628462@ec2-54-235-204-221.compute-1.amazonaws.com:5432/d80u9t8om01fml')

我收到此錯誤:

/app/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg.rb:45:在initialize': could not connect to server: No such file or directory (PG::ConnectionBad) Is the server running locally and accepting from /app/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg.rb:45:in在Unix域套接字“ / var / run”上的新連接中initialize': could not connect to server: No such file or directory (PG::ConnectionBad) Is the server running locally and accepting from /app/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg.rb:45:in /postgresql/.s.PGSQL.5432”?

我不知道如何從Heroku中訪問DATABASE_URL ENV配置變量。 我什至嘗試將實際的字符串替換為XXXXX並將其粘貼到代碼中,並引發相同的錯誤。

對於如何在Rails應用程序中執行此操作,我在StackOverflow上看到了很多建議,但對於作為獨立的紅寶石工作者如何執行此操作,則沒有任何建議。

謝謝!

編輯:將實際的數據庫信息添加到帖子中。 我知道這是不安全的,但那里沒有任何敏感內容,一旦我弄清楚了,我將對其進行更改。

當不使用Rails時, 續集是連接查詢並將查詢發送到數據庫的好方法。

從文檔:

DB = Sequel.connect('postgres://user:password@host:port/database_name') # requires pg

連接字符串由用戶名,密碼,主機,端口和數據庫名組成,但是您要將整個字符串作為“ dbname”傳遞給PG.connect。

我懷疑您會使用其他參數名稱(uri?)傳遞該字符串,或將其分解為此處第73行列出的單獨組件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM