Re: How can i pass variable in dblink_connect_u ?

From: Joe Conway <mail(at)joeconway(dot)com>
To: aditya desai <admad123(at)gmail(dot)com>, pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: How can i pass variable in dblink_connect_u ?
Date: 2022-02-01 13:41:08
Message-ID: cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 2/1/22 05:42, aditya desai wrote:
> Hi All,
> how can I pass variables in dblink_connect_u ?
>
> Current code hardcoded DB name:
> perform * from dblink_connect_u('myconn','dbname=mydb');
>
> Expected:
> databasename=current_database();
> perform * from dblink_connect_u('myconn','dbname=databasename');
>
> databasename is getting considered as text.

Correct, it is a string literal. So just create the string literal using
string manipulation functionality -- e.g.:

SELECT
dblink_connect_u('myconn','dbname=' ||
current_database());
dblink_connect_u
------------------
OK
(1 row)

HTH,

Joe

--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jian He 2022-02-01 14:18:26 Re: Offline HTML manual.
Previous Message Tatsuo Ishii 2022-02-01 13:19:52 Re: Offline HTML manual.