PostgreSQL#

The module lets Angie answer HTTP requests with data from a PostgreSQL database. The postgres_query directive builds an SQL query from the request data, such as its arguments. The postgres_pass directive sends the query to a group of database servers defined in an upstream block, and the query result becomes the response body. The postgres_output directive sets the format of the result: rds by default, text in the example below. The module does not proxy the traffic of PostgreSQL clients.

Installation#

To install the module, use one of the following packages:

  • Angie: angie-module-postgres

  • Angie PRO: angie-pro-module-postgres

Loading the Module#

Load the module in the main{} context:

load_module modules/ngx_postgres_module.so;

Configuration Example#

http {
    upstream database {
        postgres_server 127.0.0.1 dbname=ang_test
                         user=ang_test password=ang_test;
    }

    server {
        listen 80;
        postgres_output text;

        location /create {
            postgres_pass database;
            postgres_query "CREATE TABLE cats (id integer, name text)";
        }

        location /insert {
            if ($arg_id !~ "^[0-9]+$") { return 400; }
            if ($arg_name !~ "^[A-Za-z0-9 ]+$") { return 400; }

            postgres_pass database;
            postgres_query "INSERT INTO cats (id, name) VALUES ($arg_id, '$arg_name')";
        }

        location /select {
            if ($arg_id !~ "^[0-9]+$") { return 400; }

            postgres_pass database;
            postgres_query "SELECT name FROM cats WHERE id=$arg_id";
        }
    }
}

The module puts the values of request variables into the query as they are, without escaping, so an argument can change the query itself (SQL injection). Check each argument before the query, as the if directives in the example do, or allow only trusted clients to reach these locations.

Additional Information#

Version: 1.0rc7

Detailed documentation and source code are available at: FRiCKLE/ngx_postgres