leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
391 stars 93 forks source link

host cannot use hostname #74

Closed gzliudan closed 2 years ago

gzliudan commented 6 years ago

cannot connect database when use hostname, such as:

local pg = pgmoon.new({ host = "server1", port = "5432", database = "mydb", user = "postgres" })

puleeno commented 5 years ago

I have same an issue when use hostname

Logs

Ping hostname

root@332bce82ae0a:~# ping dbhostname
PING dbhostname (172.23.0.3) 56(84) bytes of data.
64 bytes from myapp_postgres_1.myapp_default (172.23.0.3): icmp_seq=1 ttl=64 time=0.276 ms
64 bytes from myapp_postgres_1.myapp_default (172.23.0.3): icmp_seq=2 ttl=64 time=0.192 ms
64 bytes from myapp_postgres_1.myapp_default (172.23.0.3): icmp_seq=3 ttl=64 time=0.236 ms
^C
--- dbhostname ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2019ms
rtt min/avg/max/mdev = 0.192/0.234/0.276/0.038 ms
root@332bce82ae0a:~#

Pgmoon config

local pgmoon = require("pgmoon")
    local pg = pgmoon.new({
    host = "dbhostname",
    port = "5432",
    database = "pl_myapp_v1",
    user = "postgres"
    })

Error logs

no resolver defined to resolve "dbhostname"
stack traceback:
coroutine 0:
    [C]: in function 'assert'
    /usr/local/openresty/lualib/myapp/init.lua:22: in function 'app'
    content_by_lua(default.conf:32):2: in function <content_by_lua(default.conf:32):1>, client: 172.23.0.1, server: localhost, request: "GET / HTTP/1.1", host: "web1.myapp.io"
2019/03/23 03:33:31 [error] 6#6: *16 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/myapp/init.lua:22: no resolver defined to resolve "dbhostname"
stack traceback:
coroutine 0:
    [C]: in function 'assert'
    /usr/local/openresty/lualib/myapp/init.lua:22: in function 'app'
    content_by_lua(default.conf:32):2: in function <content_by_lua(default.conf:32):1>, client: 172.23.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "web1.myapp.io", referrer: "http://web1.myapp.io/"
puleeno commented 5 years ago

@gzliudan I find a temporary solution to fix this issue at https://blog.wnotes.net/blog/article/lua-library-from-etc-hosts In this article suggest use lua-local-resolve to resolve local hostname

gzliudan commented 5 years ago

thank you very much.

leafo commented 2 years ago

This is an nginx/openresty specific issue: cosockets will use whaterver resolver you have provided in your nginx config. AFAIK pgmoon has no responsibility here.