首页 > 解决方案 > psycopg2.OperationalError:无法将主机名“joao”转换为地址:名称解析暂时失败

问题描述

请注意,我不使用 docker。

所以,最近,我一直在从事一个个人项目,该项目基本上是用 python (Tkinter) 编写的,并且有一个 PostgreSQL 数据库。

但是每次我运行它,我都会收到这个错误:

Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "init.py", line 305, in <lambda>
    plugin_navbar.add_command(label="Manage", command=lambda: manage_plugins(None))
  File "init.py", line 253, in manage_plugins
    con = psycopg2.connect(host=getpass.getuser(), database='db', user='postgres', password='postgres')
  File "/home/joao/.local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "joao" to address: Temporary failure in name resolution

我不知道如何修复它,这是功能的来源:

def send_plugins(self):
    con = psycopg2.connect(host=getpass.getuser(), database='db', user='postgres', password='postgres')
    
    cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
    
    metayaml = open(os.path.join(os.path.split(foldname)[-1], 'meta.yaml'))
    
    for n in os.listdir():
        cursor.execute("INSERT INTO db (name, version) VALUES ({os.path.split(foldname)[-1]}, {yaml.safe_load(metayaml)['version']);")
    
    cursor.commit()
    
    cursor.close()
    
    con.close()
        
def add_plugin(self):
    global foldname
    foldname = path.realpath(filedialog.askdirectory())
    
    filesinfold = os.listdir(foldname)
    
    os.chdir('plugin')
    os.mkdir(os.path.split(foldname)[-1])
    os.chdir(os.path.split(foldname)[-1])
    for n in filesinfold:
        shutil.move(os.path.abspath(n), os.getcwd())
    os.chdir(os.path.join(os.getcwd(), os.pardir))
        
def manage_plugins(self):
    proceed = bool(messagebox.askyesno("Proceed", "Do you want to proceed? Note that this opens the plugin manager."))  
    
    if proceed == True:
        top = Toplevel()
        
        top.minsize(width=500, height=400)
        top.resizable(False, False)
        
        plugin_dir = os.listdir('plugin')
        
        btn1 = Button(top, text='Add', command=lambda: add_plugin(None))
        btn1.grid(row=0, column=0, ipadx=3)
        
        btn2 = Button(top, text='Send', command=lambda: send_plugins(None))
        btn2.grid(row=0, column=1, ipadx=3)
        
        l1 = Label(top, text='Plugins sent:')
        l1.grid(row=1, column=0, ipadx=3)
        
        c = CamelCase()
        
        con = psycopg2.connect(sdatabase='db', user='postgres', password='postgres')
    
        cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
        
        cursor.execute("SELECT * FROM db;")
        
        for index, value in cursor.fetchall():
            l = Label(top, text=c.hump(value))
            l.grid(row=index + 2, column=0, ipadx=3)
    
        cursor.commit()
    
        cursor.close()
    
        con.close()

有人可以帮帮我吗!如果是这样,我真的很感激!

标签: pythondatabasepostgresqltkinterpsycopg2

解决方案


推荐阅读