FRiCKLE / ngx_postgres

upstream module that allows nginx to communicate directly with PostgreSQL database.
http://labs.frickle.com/nginx_ngx_postgres/
BSD 2-Clause "Simplified" License
545 stars 122 forks source link

ngx.balancer and ngx_postgrest working together (read connection info from env)? #48

Closed ruslantalpa closed 8 years ago

ruslantalpa commented 8 years ago

Is there any way of making the two compatible? The usecase is that i do not want (can't) to hardcode the connection info in the config file, i have to read it from the env. For a proxy upstream i can do this

upstream myhost {
        server 127.0.0.1:3000; #dummy
        balancer_by_lua_block {
            local balancer = require 'ngx.balancer'
            local host = os.getenv('HOST')
            local port = os.getenv('PORT')
            local ok, err = balancer.set_current_peer(host, port)
            if not ok then
                return ngx.exit(500)
            end
        }
        keepalive 64;
    }

How can i have something similar with ngx_postgrest? Thank you

agentzh commented 8 years ago

It should work. What errors are you seeing?

ruslantalpa commented 8 years ago

i am not seeing errors, i can't figure out how to call set_current_pear with host/posrt/user/pass/db parameters when it takes just 2 parameters.

ruslantalpa commented 8 years ago

postgres_server ip[:port] dbname=dbname user=user password=pass vs balancer.set_current_peer(host, port)

agentzh commented 8 years ago

@ruslantalpa Oh yes, ngx_postgres provides its own postgres_server directive here. So no love.

How about just using the pgmoon Lua library that is based on ngx_lua module's nonblocking cosocket API? See

https://github.com/leafo/pgmoon

It's much more flexible than this ngx_postgres module.

ruslantalpa commented 8 years ago

Yes i will do that, just thought i could get away with something internal since i only use it to run one specific query and get the result internally using capture which.

PS: Thank you so much for your work on openresty, it's an amazing package.