<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Llibreria_libpg%3A_accedir_a_PostgreSQL</id>
	<title>Llibreria libpg: accedir a PostgreSQL - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Llibreria_libpg%3A_accedir_a_PostgreSQL"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Llibreria_libpg:_accedir_a_PostgreSQL&amp;action=history"/>
	<updated>2026-08-30T09:27:16Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=Llibreria_libpg:_accedir_a_PostgreSQL&amp;diff=5229&amp;oldid=prev</id>
		<title>Joan a 22:57, 13 abr 2010</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Llibreria_libpg:_accedir_a_PostgreSQL&amp;diff=5229&amp;oldid=prev"/>
		<updated>2010-04-13T22:57:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;*http://www.postgresql.org/docs/8.3/static/libpq.html&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libpq5 libpq-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Els fiftxes de capçalera estan a /usr/include/postgresql. Concretament, en el codi següent s'utilitza libpq-fe.h. Aquest directori també es pot trobar fent:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$  pg_config --includedir&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Per compilar:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cc -c -I/usr/include/postgresql testlibpq.c&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When linking the final program, specify the option -lpq so that the libpq library gets pulled in, as well as the option -Ldirectory to point the compiler to the directory where the libpq library resides. (Again, the compiler will search some directories by default.) For maximum portability, put the -L option before the -lpq option. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$  cc -o testlibpq testlibpq.o -L/usr/include/postgresql/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Error messages that point to problems in this area could look like the following (el que a mi em passa...).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
testlibpq.o: In function `main':&lt;br /&gt;
testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'&lt;br /&gt;
testlibpq.o(.text+0x71): undefined reference to `PQstatus'&lt;br /&gt;
testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This means you forgot -lpq.&lt;br /&gt;
&lt;br /&gt;
/usr/bin/ld: cannot find -lpq&lt;br /&gt;
&lt;br /&gt;
This means you forgot the -L option or did not specify the right directory. &lt;br /&gt;
&lt;br /&gt;
Finalment:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$  cc -o testlibpq testlibpq.o -lpq -L/usr/include/postgresql/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These examples and others can be found in the directory src/test/examples in the source code distribution. &lt;br /&gt;
&lt;br /&gt;
testlibpq.c:&lt;br /&gt;
del codi original, canvio &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
conninfo = &amp;quot;dbname = postgres&amp;quot;;&lt;br /&gt;
per&lt;br /&gt;
conninfo = &amp;quot;host=localhost dbname = postgres user=postgres password=postgres&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * testlibpq.c&lt;br /&gt;
 *&lt;br /&gt;
 *      Test the C version of libpq, the PostgreSQL frontend library.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;quot;libpq-fe.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
exit_nicely(PGconn *conn)&lt;br /&gt;
{&lt;br /&gt;
    PQfinish(conn);&lt;br /&gt;
    exit(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
    const char *conninfo;&lt;br /&gt;
    PGconn     *conn;&lt;br /&gt;
    PGresult   *res;&lt;br /&gt;
    int         nFields;&lt;br /&gt;
    int         i,&lt;br /&gt;
                j;&lt;br /&gt;
&lt;br /&gt;
    /*&lt;br /&gt;
     * If the user supplies a parameter on the command line, use it as the&lt;br /&gt;
     * conninfo string; otherwise default to setting dbname=postgres and using&lt;br /&gt;
     * environment variables or defaults for all other connection parameters.&lt;br /&gt;
     */&lt;br /&gt;
    if (argc &amp;gt; 1)&lt;br /&gt;
        conninfo = argv[1];&lt;br /&gt;
    else&lt;br /&gt;
        conninfo = &amp;quot;dbname = postgres&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    /* Make a connection to the database */&lt;br /&gt;
    conn = PQconnectdb(conninfo);&lt;br /&gt;
&lt;br /&gt;
    /* Check to see that the backend connection was successfully made */&lt;br /&gt;
    if (PQstatus(conn) != CONNECTION_OK)&lt;br /&gt;
    {&lt;br /&gt;
        fprintf(stderr, &amp;quot;Connection to database failed: %s&amp;quot;,&lt;br /&gt;
                PQerrorMessage(conn));&lt;br /&gt;
        exit_nicely(conn);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /*&lt;br /&gt;
     * Our test case here involves using a cursor, for which we must be inside&lt;br /&gt;
     * a transaction block.  We could do the whole thing with a single&lt;br /&gt;
     * PQexec() of &amp;quot;select * from pg_database&amp;quot;, but that's too trivial to make&lt;br /&gt;
     * a good example.&lt;br /&gt;
     */&lt;br /&gt;
&lt;br /&gt;
    /* Start a transaction block */&lt;br /&gt;
    res = PQexec(conn, &amp;quot;BEGIN&amp;quot;);&lt;br /&gt;
    if (PQresultStatus(res) != PGRES_COMMAND_OK)&lt;br /&gt;
    {&lt;br /&gt;
        fprintf(stderr, &amp;quot;BEGIN command failed: %s&amp;quot;, PQerrorMessage(conn));&lt;br /&gt;
        PQclear(res);&lt;br /&gt;
        exit_nicely(conn);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /*&lt;br /&gt;
     * Should PQclear PGresult whenever it is no longer needed to avoid memory&lt;br /&gt;
     * leaks&lt;br /&gt;
     */&lt;br /&gt;
    PQclear(res);&lt;br /&gt;
&lt;br /&gt;
    /*&lt;br /&gt;
     * Fetch rows from pg_database, the system catalog of databases&lt;br /&gt;
     */&lt;br /&gt;
    res = PQexec(conn, &amp;quot;DECLARE myportal CURSOR FOR select * from pg_database&amp;quot;);&lt;br /&gt;
    if (PQresultStatus(res) != PGRES_COMMAND_OK)&lt;br /&gt;
    {&lt;br /&gt;
        fprintf(stderr, &amp;quot;DECLARE CURSOR failed: %s&amp;quot;, PQerrorMessage(conn));&lt;br /&gt;
        PQclear(res);&lt;br /&gt;
        exit_nicely(conn);&lt;br /&gt;
    }&lt;br /&gt;
    PQclear(res);&lt;br /&gt;
&lt;br /&gt;
    res = PQexec(conn, &amp;quot;FETCH ALL in myportal&amp;quot;);&lt;br /&gt;
    if (PQresultStatus(res) != PGRES_TUPLES_OK)&lt;br /&gt;
    {&lt;br /&gt;
        fprintf(stderr, &amp;quot;FETCH ALL failed: %s&amp;quot;, PQerrorMessage(conn));&lt;br /&gt;
        PQclear(res);&lt;br /&gt;
        exit_nicely(conn);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /* first, print out the attribute names */&lt;br /&gt;
    nFields = PQnfields(res);&lt;br /&gt;
    for (i = 0; i &amp;lt; nFields; i++)&lt;br /&gt;
        printf(&amp;quot;%-15s&amp;quot;, PQfname(res, i));&lt;br /&gt;
    printf(&amp;quot;\n\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    /* next, print out the rows */&lt;br /&gt;
    for (i = 0; i &amp;lt; PQntuples(res); i++)&lt;br /&gt;
    {&lt;br /&gt;
        for (j = 0; j &amp;lt; nFields; j++)&lt;br /&gt;
            printf(&amp;quot;%-15s&amp;quot;, PQgetvalue(res, i, j));&lt;br /&gt;
        printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    PQclear(res);&lt;br /&gt;
&lt;br /&gt;
    /* close the portal ... we don't bother to check for errors ... */&lt;br /&gt;
    res = PQexec(conn, &amp;quot;CLOSE myportal&amp;quot;);&lt;br /&gt;
    PQclear(res);&lt;br /&gt;
&lt;br /&gt;
    /* end the transaction */&lt;br /&gt;
    res = PQexec(conn, &amp;quot;END&amp;quot;);&lt;br /&gt;
    PQclear(res);&lt;br /&gt;
&lt;br /&gt;
    /* close the connection to the database and cleanup */&lt;br /&gt;
    PQfinish(conn);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
i el resultats és:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
datname        datdba         encoding       datcollate     datctype       datistemplate  datallowconn   datconnlimit   datlastsysoid  datfrozenxid   dattablespace  datconfig      datacl         &lt;br /&gt;
&lt;br /&gt;
template1      10             6              es_ES.UTF-8    es_ES.UTF-8    t              t              -1             11563          648            1663                          {=c/postgres,postgres=CTc/postgres}&lt;br /&gt;
template0      10             6              es_ES.UTF-8    es_ES.UTF-8    t              f              -1             11563          648            1663                          {=c/postgres,postgres=CTc/postgres}&lt;br /&gt;
postgres       10             6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                                         &lt;br /&gt;
foodmart       10             6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                                         &lt;br /&gt;
hibernate      146714         6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                          {=Tc/hibuser,hibuser=CTc/hibuser}&lt;br /&gt;
quartz         146763         6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                          {=Tc/pentaho_user,pentaho_user=CTc/pentaho_user}&lt;br /&gt;
sampledata     10             6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                                         &lt;br /&gt;
openbravo      245020         6              es_ES.UTF-8    es_ES.UTF-8    f              t              -1             11563          648            1663                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Per tant, resumint:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cc -c -I/usr/include/postgresql testlibpq.c&lt;br /&gt;
$  cc -o testlibpq testlibpq.o -lpq -L/usr/include/postgresql/&lt;br /&gt;
$ ./testlibpq&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>