简体   繁体   English

pg_dump:错误:无法将主机名“localhost”转换为地址:未知服务器错误

[英]pg_dump: error: could not translate host name "localhost" to address: Unknown server error

I have a code that should do a backup of existing PostgreSQL, but when I run the code am getting an error我有一个代码应该对现有的 PostgreSQL 进行备份,但是当我运行代码时出现错误

pg_dump: error: could not translate hostname "localhost" to address: Unknown server error pg_dump:错误:无法将主机名“localhost”转换为地址:未知服务器错误

I kept the folder path of bin and lib in an environment variable, but it is still showing the same error, Can anyone help me in this, please我将 bin 和 lib 的文件夹路径保存在环境变量中,但它仍然显示相同的错误,请任何人帮助我

below is the code:下面是代码:

import subprocess
import os

os.chdir('C:/Program Files/PostgreSQL/14/bin')

DB_NAME = 'postgress'  # your db name

DB_USER = 'postgres' # you db user
DB_HOST = "localhost"
DB_PASSWORD = 'ABC'# your db password
dump_success = 0
print('Backing up %s database ' % (DB_NAME))

command_for_dumping = f'pg_dump --host={DB_HOST} ' \
            f'--dbname={DB_NAME} ' \
            f'--username={DB_USER} ' \
            f'--no-password ' \
            f'--file=tmp/backup.dmp '
try:
     proc = subprocess.Popen(command_for_dumping, shell=True, env={
                   'PGPASSWORD': DB_PASSWORD
                   })
     proc.wait()

except Exception as e:
        dump_success = 0
        print('Exception happened during dump %s' %(e))


if dump_success:
    print('db dump successfull')
print(' restoring to a new database database')


backup_file = 'tmp/backup.dmp'
"""give absolute path of your dump file. This script will create the backup.dmp in the same directory from which u are running the script """

if not dump_success:
    print('dump unsucessfull. retsore not possible')
else:
    try:
        process = subprocess.Popen(
                        ['pg_restore',
                         '--no-owner',
                         '--dbname=postgresql://{}:{}@{}:{}/{}'.format('postgres',#db user
                                                                       'ABC', #db password
                                                                       'localhost',  #db host
                                                                       '5432', 'ReplicaDB'), #db port ,#db name
                         '-v',
                         backup_file],
                        stdout=subprocess.PIPE
                    )
        output = process.communicate()[0]
    except Exception as e:
           print('Exception during restore %e' %(e) )

So in Output it shows an error:因此在 Output 中显示错误:

pg_dump: error: could not translate host name "localhost" to address: Unknown server error pg_dump:错误:无法将主机名“localhost”转换为地址:未知服务器错误

I am new to this language, So am not sure of mistake i did.我是这种语言的新手,所以我不确定我是否犯了错误。 can anyone please guide me.谁能指导我。

It's probably a bit late for OP but hopefully this may help others. OP 可能有点晚了,但希望这可以帮助其他人。

This seems to only happen on Windows.这似乎只发生在 Windows 上。 Try adding SYSTEMROOT to env:尝试将SYSTEMROOT添加到 env:

env={
    'PGPASSWORD': DB_PASSWORD,
    'SYSTEMROOT': os.environ['SYSTEMROOT']
}

This fixed it for me calling pg_dump from python in a similar way.这为我修复了它,以类似的方式从 python 调用pg_dump I first tried using IP address directly as host, then had a different error:我首先尝试使用 IP 地址直接作为主机,然后有一个不同的错误:

Socket: The requested service provider could not be loaded or initialized

Searching for this error led me to this post: https://travis-ci.community/t/socket-the-requested-service-provider-could-not-be-loaded-or-initialized/1127搜索此错误导致我看到此帖子: https://travis-ci.community/t/socket-the-requested-service-provider-could-not-be-loaded-or-initialized/1127

Which suggested SYSTEMROOT environment variable is needed for DLLs to correctly load regarding opening socket. DLL 需要哪个建议的 SYSTEMROOT 环境变量才能正确加载有关打开套接字的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将主机名“postgres”转换为地址:未知主机 - could not translate host name “postgres” to address: Unknown host Postgres 错误无法将主机名“postgres”转换为地址:名称或服务未知 - Postgres error could not translate host name "postgres" to address: Name or service not known docker 中的 pg_dump 错误但仍然成功执行 - pg_dump error in docker but still executes successfully pg_dump:命令行参数太多(首先是“--host=localhost”) - pg_dump: too many command-line arguments (first is “--host=localhost”) 发生异常:OperationalError 无法将主机名“db”转换为地址:未知主机 - Exception has occurred: OperationalError could not translate host name “db” to address: Unknown host psycopg2.OperationalError:无法将主机名“xxxxxx.us-east-1.rds.amazonaws.com”转换为地址:未知主机 - psycopg2.OperationalError: could not translate host name "xxxxxx.us-east-1.rds.amazonaws.com" to address: Unknown host Django:dbbackup 显示 pg_dump:错误:命令行太多 arguments - Django: dbbackup displays pg_dump: error: too many command-line arguments 无法将主机名“db”转换为地址:名称解析暂时失败 - could not translate host name "db" to address: Temporary failure in name resolution python中的pg_dump密码 - pg_dump password in python Docker:运行容器时“无法将主机名“plagdb”转换为地址” - Docker: "could not translate host name "plagdb" to address" when running a container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM