libpq analog of \c
libpq analog of \c
От:
Igor Korot <ikorot01@gmail.com>
Дата:
Hi, What do issue in the libpq interface to select the current database? Thank you.
Re: libpq analog of \c
От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
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.
Re: libpq analog of \c
От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
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
Re: libpq analog of \c
От:
Igor Korot <ikorot01@gmail.com>
Дата:
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
Re: libpq analog of \c
От:
Adrian Klaver <adrian.klaver@aklaver.com>
Дата:
On 10/5/26 7:57 AM, Igor Korot wrote: > Hi, > > > > On Sun, Oct 4, 2026, 11:35 PM Laurenz Albe > 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
Re: libpq analog of \c
От:
Igor Korot <ikorot01@gmail.com>
Дата:
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
Re: libpq analog of \c
От:
Juan Rodrigo Alejandro Burgos Mella <rodrigoburgosmella@gmail.com>
Дата:
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;
}
#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.