Re: libpq analog of \c - Mailing list pgsql-general

From Juan Rodrigo Alejandro Burgos Mella
Subject Re: libpq analog of \c
Date
Msg-id CAHbZ42xNjUmtkhGptghGa3OsLQSFU8J+En271+CBZN_XRYCrwQ@mail.gmail.com
Whole thread
In response to libpq analog of \c  (Igor Korot <ikorot01@gmail.com>)
List pgsql-general
It is entirely possible; multiple connections can be made simultaneously, but obviously it will depend on the resources of the server where the binary is running.

here's an example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

    EXEC SQL BEGIN DECLARE SECTION;
        char conn_str1[256], host1[30], port1[30], ddbb1[30], user1[30], pass1[30];
        char conn_str2[256], host2[30], port2[30], ddbb2[30], user2[30], pass2[30];
        char dbname[100];
    EXEC SQL END DECLARE SECTION;

strcpy(host1,"server1.instucion.com");
strcpy(port1,"5433");
strcpy(ddbb1,"ddbb_prod");
strcpy(user1,"user1conect");
strcpy(pass1,"c1d44n");

sprintf(conn_str1,"%s@%s:%s", ddbb1, host1, port1);

EXEC SQL CONNECT TO :conn_str1 AS conn1 USER :user1 USING :pass1;
    if (sqlca.sqlcode != 0) {
        printf("Error conectando a ddbb1: %s\n", sqlca.sqlerrm.sqlerrmc);
        exit(1);
    }

strcpy(host2,"10.91.124.18");
strcpy(port2,"5432");
strcpy(ddbb2,"ddbb_dev");
strcpy(user2,"develop1");
strcpy(pass2,"xcc134q3_#aw");

sprintf(conn_str2,"%s@%s:%s", ddbb2, host2, port2);

EXEC SQL CONNECT TO :conn_str2 AS conn2 USER :user2 USING :pass2;
    if (sqlca.sqlcode != 0) {
        printf("Error conectando a ddbb2: %s\n", sqlca.sqlerrm.sqlerrmc);
        exit(1);
    }

    EXEC SQL AT conn1 SELECT current_database() INTO :dbname;
    if (sqlca.sqlcode != 0) {
        printf("Error SELECT en ddbb1: %s\n", sqlca.sqlerrm.sqlerrmc);
        exit(1);
    }
printf("DDBB 1: [%s]\n", dbname);

    EXEC SQL AT conn2 SELECT current_database() INTO :dbname;
    if (sqlca.sqlcode != 0) {
        printf("Error SELECT en ddbb2: %s\n", sqlca.sqlerrm.sqlerrmc);
        exit(1);
    }
printf("DDBB2 : [%s]\n", dbname);

    EXEC SQL DISCONNECT conn1;
    EXEC SQL DISCONNECT conn2;

return 0;
}

Atte.
JRBM

El lun, 5 oct 2026 a las 1:11, Igor Korot (<ikorot01@gmail.com>) escribió:
Hi,
What do  issue in the libpq interface to
select the current database?

Thank you.


pgsql-general by date:

Previous
From: Igor Korot
Date:
Subject: Re: libpq analog of \c
Next
From: Thiemo Kellner
Date:
Subject: Re: Why is materialized view creation a "security-restricted operation"?