首页 > 解决方案 > 当我尝试使用 asyncpg 连接我的 postgress sql13 时出现错误

问题描述

第一个块是我在第二个块中运行代码时遇到的错误。

第三块是我的 NCA 文件

问题是当我尝试连接 PostgreSQL sql13 来存储数据时,我收到了上面的错误,所以有人可以帮我解决这个问题吗?我尝试了很多来修复它,但没有任何效果。这是我得到的错误

    Traceback (most recent call last):
    File "C:\Users\Steve Sony\Downloads\NCA-main\run.py", line 29, in <module>
    bot = NCA()
    File "C:\Users\Steve Sony\Downloads\NCA-main\NCA.py", line 29, in __init__
    self.loop.run_until_complete(self.create_db_pool())
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
    File "C:\Users\Steve Sony\Downloads\NCA-main\NCA.py", line 32, in create_db_pool
    self.db = await asyncpg.create_pool(database = "nca",password = "hehe",user = "postgres")
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 407, in _async__init__
    await self._initialize()
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 435, in _initialize
    await first_ch.connect()
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 127, in connect
    self._con = await self._pool._get_new_connection()
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 477, in _get_new_connection
    con = await connection.connect(
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 2045, in connect
    return await connect_utils._connect(
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connect_utils.py", line 790, in _connect
    raise last_error
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connect_utils.py", line 776, in _connect
    return await _connect_addr(
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connect_utils.py", line 676, in _connect_addr
    return await __connect_addr(params, timeout, True, *args)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connect_utils.py", line 720, in __connect_addr
    tr, pr = await compat.wait_for(connector, timeout=timeout)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\compat.py", line 66, in wait_for
    return await asyncio.wait_for(fut, timeout)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 481, in wait_for
    return fut.result()
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connect_utils.py", line 586, in _create_ssl_connection
    tr, pr = await loop.create_connection(
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1061, in create_connection
    raise exceptions[0]
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 702, in sock_connect
    return await self._proactor.connect(sock, address)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 812, in _poll
    value = callback(transferred, key, ov)
    File "C:\Users\Steve Sony\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 599, in finish_connect
    ov.getresult()
ConnectionRefusedError: [WinError 1225] The remote computer refused the network connection
[Finished in 8.1s]

运行连接代码的主文件:

import discord
from discord.ext import commands
import json
import os
import asyncpg
from datetime import datetime
import aiosqlite
import asyncio
from dotenv import load_dotenv
from tortoise import Tortoise
from utilities import Tag
import config
from help_commands import HelpCommand
from NCA import NCA
import cogs
import jishaku
load_dotenv()


Tag = Tag

os.environ.setdefault("JISHAKU_HIDE", "1")
os.environ.setdefault("JISHAKU_RETAIN", "1")
os.environ.setdefault("JISHAKU_NO_UNDERSCORE", "1")


if __name__ == "__main__":

    bot = NCA()

for name in os.listdir("./cogs"):   
    if name.endswith(".py"):
        bot.load_extension("cogs.{}".format(name[:-3]))

@bot.event
async def on_ready():
    await bot.db.execute("CREATE TABLE IF NOT EXISTS warnlogs (guild_id BIGINT, member_id BIGINT, warns TEXT[], times DECIMAL[])")
    print(f'Logged in as: {bot.user.name}')
    print(f'With ID: {bot.user.id}')
    print('I am ready to use sir')
    print('____________________________')
    db_url = "sqlite://data/main.db"

    await Tortoise.init(db_url=db_url, modules={"models": ["utilities"]})

    await Tortoise.generate_schemas()


@bot.event
async def on_message(message):

    if f"<@{bot.user.id}>" in message.content or f"<@!{bot.user.id}" in message.content:
        await message.channel.send(
            f"""Hi,{message.author.mention} you pinged me 
if you need help type ``{config.PREFIX}help``"""
        )

    await bot.process_commands(message)


bot.load_extension('jishaku')


bot.run("hmm")

NCA 文件:

import discord
from discord.ext import commands
import asyncpg
import os
from help_commands import HelpCommand
import config
import cogs

status = discord.Status.idle
activity = discord.Activity(
    type=discord.ActivityType.listening, name=f"The prefix  ( {config.PREFIX} )"
)

class NCA(commands.Bot):
    def __init__(self,**options):
        super().__init__(
            command_prefix=config.PREFIX,
            intents=discord.Intents(members=True, messages=True, guilds=True, reactions=True),
            status=status,
            activity=activity,
            help_command=HelpCommand(),
            description=config.DESCRIPTION,
            name=config.NAME,
            allowed_mentions = discord.AllowedMentions(everyone = False, roles = False),
            )

        
    
        self.loop.run_until_complete(self.create_db_pool())

    async def create_db_pool(self):
        self.db = await asyncpg.create_pool(database = "nca",password = "hehe",user = "postgres")

标签: pythonpostgresqldiscord.py

解决方案


推荐阅读