ECPG

A preprocessor package to embed SQL statements in C code

ECPG ("Embedded C for PostgreSQL") is a preprocessor package which enables SQL statements to be embedded in C (or possibly C++ code, though C++ support is incomplete). ECPG is included in the PostgreSQL base distribution.

ECPG was added in PostgreSQL 6.3 (initial commit 82034103), though it appears to be based on code previously provided for Postgres95.

Usage

ECPG is used by creating a source file with an extension other than ".c" - typically ".pgc" is used. Running ecpg on this source file will create a source file with a ".c" extension, which can then be compiled as usual - remember to include the "-l ecpg" flag.

ECPG also has an "Informix compatibility mode", which when active makes it try and behave as if it were the Informix precompiler for Informix E/SQL. However compatibility is limited and ECPG cannot serve as a drop-in compiler for porting Informix code to PostgreSQL.

Change history

Examples

Minimal ECPG "Hello World" code example:

#include  <stdio.h>

EXEC SQL BEGIN DECLARE SECTION;
    char output[1024];
EXEC SQL END DECLARE SECTION;

int
main()
{
    EXEC SQL CONNECT TO testdb AS con1 USER testuser;

    EXEC SQL SELECT 'hello world' INTO :output;
    printf("%s\n", output);

    EXEC SQL DISCONNECT ALL;
    return 0;
}

Categories

Application interface