iT邦幫忙

0

Heroku 上的postgres 是如何連接的?

  • 分享至 

  • xImage

我想用我的line連接到數據庫那裡去,卻一直出現錯誤。 下面代碼是在jupyter裡面測試寫也是按照 https://ithelp.ithome.com.tw/articles/10219773 的流程。


import os
import psycopg2

DATABASE_URL = os.popen('heroku config:get DATABASE_URL -a bot-all-day-long').read()[:-1]

conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()

query = 'SELECT table_name FROM INFORMATION_SCHEMA.TABLES'
cursor.execute(query)

cursor.fetchone()

錯誤訊息是:

OperationalError                          Traceback (most recent call last)
<ipython-input-20-97d5072dff48> in <module>
      4 DATABASE_URL = os.popen('heroku config:get DATABASE_URL -a bot-all-day-long').read()[:-1]
      5 
----> 6 conn = psycopg2.connect(DATABASE_URL, sslmode='require')
      7 cursor = conn.cursor()
      8 

/srv/conda/envs/notebook/lib/python3.6/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
    120 
    121     dsn = _ext.make_dsn(dsn, **kwargs)
--> 122     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    123     if cursor_factory is not None:
    124         conn.cursor_factory = cursor_factory

OperationalError: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
 不知道該怎麼辦。
cloudyang iT邦新手 5 級 ‧ 2021-07-08 14:30:19 檢舉
當初我的line bot用psycopg2都一直跑錯誤訊息,但用"from flask_sqlalchemy import SQLAlchemy"就可以,可以參考看看
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答