且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

psql:无法翻译主机名“ somePostgres”,地址:未知的名称或服务

更新时间:2022-11-06 21:10:50

此错误的基本含义是psql无法解析主机名,请改用ip地址。

Basically what this error means is that psql was unable to resolve the host name, try using the ip address instead.

https://github.com/postgres/postgres/blob/313f56ce2d1b9dfd3483e4f39611baa27852835a/src/interfaces/libpq/fe-connect.c#L2275-L2285

case CHT_HOST_NAME:
    ret = pg_getaddrinfo_all(ch->host, portstr, &hint,
                             &conn->addrlist);
    if (ret || !conn->addrlist)
    {
        appendPQExpBuffer(&conn->errorMessage,
                          libpq_gettext("could not translate host name \"%s\" to address: %s\n"),
                          ch->host, gai_strerror(ret));
        goto keep_going;
    }
break;

https://github.com/postgres/postgres/blob/8255c7a5eeba8f1a38b7a431c04909bde4f5e67d/src/common/ip.c#L57-L75

int
pg_getaddrinfo_all(const char *hostname, const char *servname,
                   const struct addrinfo *hintp, struct addrinfo **result)
{
    int         rc;

    /* not all versions of getaddrinfo() zero *result on failure */
    *result = NULL;

#ifdef HAVE_UNIX_SOCKETS
    if (hintp->ai_family == AF_UNIX)
        return getaddrinfo_unix(servname, hintp, result);
#endif

    /* NULL has special meaning to getaddrinfo(). */
    rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
                     servname, hintp, result);

    return rc;
}