我如何 运行 在 heroku 上为我的 discord.py 机器人使用 postgres?

How do I run postgres on heroku for my discord.py bot?

所以我正在使用 heroku(目前)来托管我的 discord.py 机器人。 最近开始用postgres做数据库,这是连接代码:

我正在使用此代码进行连接:

import asyncpg

async def create_db_pool():
    client.pg_con = await asyncpg.create_pool(DATABASE_URL) #db url goes here

client.loop.run_until_complete(create_db_pool())
client.run(TOKEN)

当我在本地 运行 代码工作得很好,但是当我使用 heroku 时显示连接错误。

我也添加了 'Heroku-postgres' 作为附加组件,但它仍然不起作用? 请分享相同的解决方案。

提前致谢:)

client.pg_con = await asyncpg.create_pool(DATABASE_URL) #db url goes here

当您添加 Postgres DB 插件时,Heroku 会向您的应用程序添加一个环境变量。它的值包含一个数据库url。你需要找回那个。类似于:

import os
DATABASE_URL = os.environ.get("ENV_VAR_NAME_HERE", None)

@ŁukaszKwieciński I don't want to save the data in the heroku database, I am using the default server of postgres i.e. pgAdmin4 to save data.

您的 Heroku 应用程序未附带数据库。保存在 Heroku 应用程序上的任何内容都将被擦除,请参阅 here。您的 Heroku 应用程序上没有“postgres 的默认服务器”。这就是为什么您要将(第三方)附加组件添加到充当数据库的应用程序:postgres 附加组件。