Psql Command Line Password and Query

Viewed 3714

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:postgres@localhost/dbname -c 'select * from view'

But this is the same.

Do you have any idea where i am misplaying this ?

Thanks !

3 Answers

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:password@host/dbname

Take care about your double quotes too.

Bye,

You have to use double quotes, not single quotes, to protect a string with spaces in Windows' cmd.exe.

psql postgresql://myusername:myPassword@127.0.0.1:5432/myDB
Related