[英]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.