postgres connection with port option in shell script

Started by Abhra Karabout 8 years ago4 messagesgeneral
Jump to latest
#1Abhra Kar
abhra.kar@gmail.com

Hi,

I want to get postgres connection in script file. I am executing
below command and successfully getting connected ---

psql postgresql://$USER:$PASSWORD@$HOST/$DATABASE <<END

select * from abc;

This is executing with default 5432 port.If postgres is installed to any
other port then port option need to be include here[Don't want to take port
from .pgpass file].

How I can modify this command with PORT option.

Thanks

#2Andreas Kretschmer
andreas@a-kretschmer.de
In reply to: Abhra Kar (#1)
Re: postgres connection with port option in shell script

Am 14.02.2018 um 16:21 schrieb Abhra Kar:

Hi,

      I want to get postgres connection in script file. I am executing
below command and successfully getting connected ---

psql postgresql://$USER:$PASSWORD@$HOST/$DATABASE <<END

select * from abc;

This is executing with default 5432 port.If postgres is installed to
any other port then port option need to be include here[Don't want to
take port from .pgpass file].

How I can modify this command with PORT option.

Thanks

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]

Rgards, Andreas

--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com

#3David G. Johnston
david.g.johnston@gmail.com
In reply to: Abhra Kar (#1)
Re: postgres connection with port option in shell script

On Wed, Feb 14, 2018 at 8:21 AM, Abhra Kar <abhra.kar@gmail.com> wrote:

Hi,

I want to get postgres connection in script file. I am executing
below command and successfully getting connected ---

psql postgresql://$USER:$PASSWORD@$HOST/$DATABASE <<END

If all you are going to do is substitute environment variables into a URI
why not just identify the environment variables that psql uses directly,
set those, and call "psql" directly.​

select * from abc;

This is executing with default 5432 port.If postgres is installed to any
other port then port option need to be include here[Don't want to take port
from .pgpass file].

​You couldn't even if you wanted to.​ Did you maybe mean the
.pg_service.conf file?

You should strongly consider using .pgpass instead of "$PASSWORD" -
especially depending upon where perform the export.

How I can modify this command with PORT option.


https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-CONNSTRING

​David J.

#4Abhra Kar
abhra.kar@gmail.com
In reply to: David G. Johnston (#3)
Re: postgres connection with port option in shell script

On Wed, Feb 14, 2018 at 9:03 PM, David G. Johnston <
david.g.johnston@gmail.com> wrote:

On Wed, Feb 14, 2018 at 8:21 AM, Abhra Kar <abhra.kar@gmail.com> wrote:

Hi,

I want to get postgres connection in script file. I am executing
below command and successfully getting connected ---

psql postgresql://$USER:$PASSWORD@$HOST/$DATABASE <<END

If all you are going to do is substitute environment variables into a URI
why not just identify the environment variables that psql uses directly,
set those, and call "psql" directly.​

select * from abc;

This is executing with default 5432 port.If postgres is installed to any
other port then port option need to be include here[Don't want to take port
from .pgpass file].

​You couldn't even if you wanted to.​ Did you maybe mean the
.pg_service.conf file?

You should strongly consider using .pgpass instead of "$PASSWORD" -
especially depending upon where perform the export.

How I can modify this command with PORT option.

https://www.postgresql.org/docs/10/static/libpq-connect.
html#LIBPQ-CONNSTRING

​David J.

Hi David,
I meant .pgpass file with in user home directory.And I
followed the link you provided.It worked.Thanks a lot.

Regards,
Abhra