pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
libpq analog of \c
7+ messages / 5 participants
[nested] [flat]

* libpq analog of \c
@ 2026-10-05 06:09  Igor Korot <ikorot01@gmail.com>
  0 siblings, 3 replies; 7+ messages in thread

From: Igor Korot @ 2026-10-05 06:09 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,
What do  issue in the libpq interface to
select the current database?

Thank you.





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 06:28  David G. Johnston <david.g.johnston@gmail.com>
  parent: Igor Korot <ikorot01@gmail.com>
  2 siblings, 0 replies; 7+ messages in thread

From: David G. Johnston @ 2026-10-05 06:28 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Sunday, October 4, 2026, Igor Korot <ikorot01@gmail.com> wrote:

> Hi,
> What do  issue in the libpq interface to
> select the current database?
>

I don’t think there is an analog…I suggest you read the C code for how psql
implements \connect if you want to see how it uses libpq to do its thing.

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 06:35  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: Igor Korot <ikorot01@gmail.com>
  2 siblings, 1 reply; 7+ messages in thread

From: Laurenz Albe @ 2026-10-05 06:35 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Sun, 2026-10-04 at 23:09 -0700, Igor Korot wrote:
> What do  issue in the libpq interface to
> select the current database?

You cannot select the current database.  A database connection remains
connected to the same database for the entire time of the connection.
You'll have to do what psql does: connect to the second database, and
if that succeeds, disconnect from the first one.

Yours,
Laurenz Albe






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 14:57  Igor Korot <ikorot01@gmail.com>
  parent: Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 1 reply; 7+ messages in thread

From: Igor Korot @ 2026-10-05 14:57 UTC (permalink / raw)
  To: Laurenz Albe <laurenz.albe@cybertec.at>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,



On Sun, Oct 4, 2026, 11:35 PM Laurenz Albe <laurenz.albe@cybertec.at> wrote:

> On Sun, 2026-10-04 at 23:09 -0700, Igor Korot wrote:
> > What do  issue in the libpq interface to
> > select the current database?
>
> You cannot select the current database.  A database connection remains
> connected to the same database for the entire time of the connection.
> You'll have to do what psql does: connect to the second database, and
> if that succeeds, disconnect from the first one.
>

Where do I look for that code?

Thank you.


> Yours,
> Laurenz Albe
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 15:12  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Igor Korot <ikorot01@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Adrian Klaver @ 2026-10-05 15:12 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; Laurenz Albe <laurenz.albe@cybertec.at>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 10/5/26 7:57 AM, Igor Korot wrote:
> Hi,
> 
> 
> 
> On Sun, Oct 4, 2026, 11:35 PM Laurenz Albe <laurenz.albe@cybertec.at 
> <mailto:laurenz.albe@cybertec.at>> wrote:
> 
>     On Sun, 2026-10-04 at 23:09 -0700, Igor Korot wrote:
>      > What do  issue in the libpq interface to
>      > select the current database?
> 
>     You cannot select the current database.  A database connection remains
>     connected to the same database for the entire time of the connection.
>     You'll have to do what psql does: connect to the second database, and
>     if that succeeds, disconnect from the first one.
> 
> 
> Where do I look for that code?

https://github.com/postgres/postgres/blob/master/src/bin/psql/command.c

As a starting point, section beginning with:

/*
  * \c or \connect -- connect to database using the specified parameters.
  *
  * \c [-reuse-previous=BOOL] dbname user host port

...
> 
> Thank you.
> 
> 
>     Yours,
>     Laurenz Albe
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 15:55  Igor Korot <ikorot01@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Igor Korot @ 2026-10-05 15:55 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Laurenz Albe <laurenz.albe@cybertec.at>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Thx.


On Mon, Oct 5, 2026 at 8:12 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 10/5/26 7:57 AM, Igor Korot wrote:
> > Hi,
> >
> >
> >
> > On Sun, Oct 4, 2026, 11:35 PM Laurenz Albe <laurenz.albe@cybertec.at
> > <mailto:laurenz.albe@cybertec.at>> wrote:
> >
> >     On Sun, 2026-10-04 at 23:09 -0700, Igor Korot wrote:
> >      > What do  issue in the libpq interface to
> >      > select the current database?
> >
> >     You cannot select the current database.  A database connection
> remains
> >     connected to the same database for the entire time of the connection.
> >     You'll have to do what psql does: connect to the second database, and
> >     if that succeeds, disconnect from the first one.
> >
> >
> > Where do I look for that code?
>
> https://github.com/postgres/postgres/blob/master/src/bin/psql/command.c
>
> As a starting point, section beginning with:
>
> /*
>   * \c or \connect -- connect to database using the specified parameters.
>   *
>   * \c [-reuse-previous=BOOL] dbname user host port
>
> ...
> >
> > Thank you.
> >
> >
> >     Yours,
> >     Laurenz Albe
> >
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: libpq analog of \c
@ 2026-10-05 20:09  Juan Rodrigo Alejandro Burgos Mella <rodrigoburgosmella@gmail.com>
  parent: Igor Korot <ikorot01@gmail.com>
  2 siblings, 0 replies; 7+ messages in thread

From: Juan Rodrigo Alejandro Burgos Mella @ 2026-10-05 20:09 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

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.
>
>
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2026-10-05 20:09 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-05 06:09 libpq analog of \c Igor Korot <ikorot01@gmail.com>
2026-10-05 06:28 ` David G. Johnston <david.g.johnston@gmail.com>
2026-10-05 06:35 ` Laurenz Albe <laurenz.albe@cybertec.at>
2026-10-05 14:57   ` Igor Korot <ikorot01@gmail.com>
2026-10-05 15:12     ` Adrian Klaver <adrian.klaver@aklaver.com>
2026-10-05 15:55       ` Igor Korot <ikorot01@gmail.com>
2026-10-05 20:09 ` Juan Rodrigo Alejandro Burgos Mella <rodrigoburgosmella@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox