encyclopegia.org

ECPG

ECPG is the standard, in the PostgreSQL database built-in, client programming interface for embedding SQL in programs written in the C programming language. It provides the option for accessing the PostgreSQL database directly from the C code in the application, using SQL commands.

ECPG is the standard, in the PostgreSQL database built-in, client programming interface for embedding SQL in programs written in the C programming language. It provides the option for accessing the PostgreSQL database directly from the C code in the application, using SQL commands. // ... C code ... EXEC SQL ; // ... C code ... An example how to connect to a database: EXEC SQL CONNECT TO databasename[@hostname][:port] [AS connectionname] [USER username]; The embedded SQL part will be processed through ECPG preprocessor where SQL code will be replaced with the calls to the ecpg library (libecpg.a or libecpg.so). The file will be also preprocessed with ecpg, which converts it to a .c file according to the ANSI standards. Therefore, in the second step, the generated .c file can be directly compiled with a standard C compiler. Following command will create from the my_c_file_with_embedded_sql_commands.pcg file a my_c_file_with_embedded_sql_commands.c file, which can be processed further as a pure C code. $ ecpg my_c_file_with_embedded_sql_commands.pcg There is also source code of the ecpg available in the PostgreSQL Source Code git repository. Note: While compiling the preprocessed .c code, do not forget to link the ecpg library (libepcg), so that the generated calls can find their linked methods. EXEC SQL INSERT INTO tablename VALUES (:variablename); This can be used in any statement, INSERT statement was chosen just as a simple example for illustration. The above example shows how to pass a C variable to the SQL, but data can be passed also in the opposite direction: back to the application. The following example shows how to pass value from SQL back to the application's C variable. EXEC SQL BEGIN DECLARE SECTION; VARCHAR variablename; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT columnname INTO :variablename FROM tablename; For simplicity, let's assume there is only one row in the table table name. This statement will insert the value of the column columnname into the variable variable. Every command that supports the INTO clause can be used in this way, for example the FETCH command.

References

  1. PostgreSQL Homepage · retrieved 2015-06-05 · rev 1302690385
    Cite this reference

    MLA "PostgreSQL Homepage." 2015-06-05. http://www.postgresql.org/. Accessed 24 November 2026.

    Chicago "PostgreSQL Homepage." 2015-06-05. http://www.postgresql.org/.

    APA encyclopegia.org. (2015). PostgreSQL Homepage. Retrieved November 24, 2026, from http://www.postgresql.org/

  2. Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). PostgreSQL Developer's Guide . Packt Publishing Ltd, 2015. p. 197. ISBN 978-1783989034 . Retrieved 6 June 2015 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1302690385, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1302690385
    Cite this reference

    MLA "Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). PostgreSQL Developer's Guide . Packt Publishing Ltd, 2015. p. 197. ISBN 978-1783989034 . Retrieved 6 June 2015 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1302690385, retrieved 2026-11-18." Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). 2026-11-17. https://books.google.com/books?id=qkPfBgAAQBAJ. Accessed 24 November 2026.

    Chicago "Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). PostgreSQL Developer's Guide . Packt Publishing Ltd, 2015. p. 197. ISBN 978-1783989034 . Retrieved 6 June 2015 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1302690385, retrieved 2026-11-18." Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). 2026-11-17. https://books.google.com/books?id=qkPfBgAAQBAJ.

    APA Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). (2026). Ahmed, Ibrar, Fayyaz, Asif and Shahzad, Amjad (27 February 2015). PostgreSQL Developer's Guide . Packt Publishing Ltd, 2015. p. 197. ISBN 978-1783989034 . Retrieved 6 June 2015 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1302690385, retrieved 2026-11-18. Retrieved November 24, 2026, from https://books.google.com/books?id=qkPfBgAAQBAJ

  3. SQLCA · retrieved 2018-01-22 · rev 1302690385
    Cite this reference

    MLA "SQLCA." 2018-01-22. https://www.postgresql.org/docs/current/static/ecpg-errors.html#ECPG-SQLCA. Accessed 24 November 2026.

    Chicago "SQLCA." 2018-01-22. https://www.postgresql.org/docs/current/static/ecpg-errors.html#ECPG-SQLCA.

    APA encyclopegia.org. (2018). SQLCA. Retrieved November 24, 2026, from https://www.postgresql.org/docs/current/static/ecpg-errors.html#ECPG-SQLCA

  4. CPP Linkage · retrieved 2015-06-05 · rev 1302690385
    Cite this reference

    MLA "CPP Linkage." 2015-06-05. http://en.cppreference.com/w/cpp/language/language_linkage. Accessed 24 November 2026.

    Chicago "CPP Linkage." 2015-06-05. http://en.cppreference.com/w/cpp/language/language_linkage.

    APA encyclopegia.org. (2015). CPP Linkage. Retrieved November 24, 2026, from http://en.cppreference.com/w/cpp/language/language_linkage

  5. C++ Applications · retrieved 2018-01-22 · rev 1302690385
    Cite this reference

    MLA "C++ Applications." 2018-01-22. https://www.postgresql.org/docs/current/static/ecpg-cpp.html. Accessed 24 November 2026.

    Chicago "C++ Applications." 2018-01-22. https://www.postgresql.org/docs/current/static/ecpg-cpp.html.

    APA encyclopegia.org. (2018). C++ Applications. Retrieved November 24, 2026, from https://www.postgresql.org/docs/current/static/ecpg-cpp.html

Cite this article

MLA

"ECPG." encyclopegia.org, 2025, https://encyclopegia.org/wiki/ecpg. Accessed 24 November 2026.

Chicago

"ECPG." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/ecpg.

APA

encyclopegia.org. (2025). ECPG. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/ecpg

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Buckeye Gymnastics

enwiki · references available

Hokatu

enwiki · references available

List of LGBTQ Olympians and Paralympians

enwiki · references available

Students for Fair Admissions v. Harvard

enwiki · references available

Vladimir Ilyich Lenin (poem)

enwiki · references available

Agriotheriini

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1302690385 · 0.5 · snapshot enwiki-001302690385

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.