If anyone else wonders, this is a working example of an nginx.conf file. You only need to install OpenResty with the postgres module (use the
flag).
http {
[...]
upstream pgsql_db_1 {
postgres_server 127.0.0.1 dbname=demo user=postgres password=test;
}
server {
[...]
location /pgsql_1_gate {
postgres_pass pgsql_db_1;
postgres_query $echo_request_body;
}
location /pgsql {
content_by_lua '
local sql = "select * from users"
local resp = ngx.location.capture("/pgsql_1_gate", {
method = ngx.HTTP_POST, body = sql
})
if resp.status ~= ngx.HTTP_OK or not resp.body then
error("failed to query pgsql")
end
local parser = require "rds.parser"
local res, err = parser.parse(resp.body)
if res == nil then
error("failed to parse RDS: " .. err)
end
local rows = res.resultset
if not rows or #rows == 0 then
ngx.say("empty resultset")
ngx.exit(0)
end
for i, row in ipairs(rows) do
ngx.print("row ", i, ": ")
for col, val in pairs(row) do
if val ~= parser.null then
ngx.print(col, "=", val, " ")
else
ngx.print(col, "=null ")
end
end
ngx.say()
end
';
}
}
}
r.