Psql Command Line Password and Query

Issue

I am trying to make a one line psql request. I would like to send both credentials and query in the same time.
I am using this :

psql.exe "dbname=dbname user=postgres password=postgres host=localhost" -c 'select * from table'

Actually if i take back the -c option it works but right now it sends me this :

psql: attention : option supplémentaire « 'select » ignorée
psql: attention : option supplémentaire « * » ignorée
psql: attention : option supplémentaire « from » ignorée
psql: attention : option supplémentaire « view' » ignorée

The connection to the database worked but thoose logs are in french but they mean "care : option skipped".

I tried with Url request

psql.exe postgresql://postgres:[email protected]/dbname -c 'select * from view'

But this is the same.

Do you have any idea where i am misplaying this ?

Thanks !

Solution

Ok guys, found the solution. After a better read of the –help, i figured that

psql [OPTIONS]... [NOM_BASE [NOM_UTILISATEUR]]

Connexion options are to be used after the query so instead of psql [CONNEXION] -c "blabla"
Use this

psql -c "select * from table" "dbname=dbname user=user password=password host=host"

or

psql -c "select * from table" postgresql://user:[email protected]/dbname

Take care about your double quotes too.

Bye,

Answered By – Gabriel Morin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published