Running psql from shell always ask for connection details - windows

I am trying to run psql command from windows command line. However, command always ask for connection details even though connection details are given.
I have tried below two commands but none works:
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat -f d:\test.sql postgresql://postgres:password#localhost:5432/testdb
and
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat" -h localhost -d testdb -U postgres -p 5432 -f d:\test.sql
I have created password file to store password as mentioned here.
However, in command line, it asks for host name, database and other details.

Related

How to use PGPASS file in Powershell to avoid password prompt?

I had to automate my postgre database backup. As instructed by my software vendor I am trying to use pg_dump.exe (see below) file to take a backup but that prompts me for password.
.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo
So googled and found that as per "https://www.postgresql.org/docs/9.6/libpq-pgpass.html" I can create a pgpass.conf file within 'C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf" which I did.
Then I tried to pass data of pgpass.conf file to env variable before executing my pg_dump command. But it is not working. Still I am getting prompt to enter password. This is the content of pgpass.conf file: *:*:*:postgres:password
Below is the code I am trying in PowerShell,
$Env:PGPASSFILE="C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf"
cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL\9.6\bin"
.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo
Why am I still being asked for password?
When I type following code $Env:AppData I get following response "C:\Users\User1\AppData\Roaming"
Everywhere there are guidance on how to use it in UNIX or command prompt but not in powershell. Any help is appreciated. Also if you could direct me how to secure this password file then it will be great.
With password prompt I cannot automate it with windows task scheduler.
I suspect you have a suitable solution, however, as a quick (and not secure) workaround via the command prompt, you can use the variable PGPASSWORD to hold the password then run the backup script.
A sample might be something like:
SET PGPASSWORD=password
cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL\9.6\bin" pg_dump.exe -h localhost -p 4432 -U postgres -b -F t -f "d:\qs_backup\QSR_backup.tar" QSR
Rod
I have yet to get the damned thing to work yet, but I did find this:
-w
--no-password Never issue a password prompt. If the server requires password authentication and a password is not available by other means
such as a .pgpass file, the connection attempt will fail. This option
can be useful in batch jobs and scripts where no user is present to
enter a password.
I don't see a -w parameter in your call to pg_dump
I used pg_hba file to allow connection "trust" this is riskier method but I had to get things done ASAP. Thank you for your time and effort

pg_restore ignores .pgpass and PGPASSWORD environment variable

I want to import a backup using pg_restore without a password prompt.
I tried several options but after I run the script it will always ask for a password. pg_dump is working but not pg_restore. I can run the pg_restore command if I enter my password but I want a passwordless command or at least I don't want to enter my password because the script has to work without user interaction.
What is working for me:
PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql
What is NOT working
1.)
PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql
2.)
pg_restore --dbname=postgresql://user:pass#localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql
3.)
touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql
any ideas?
Regards
As per the doc, -W will prompt for a password. -w will not
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as
a .pgpass file, the connection attempt will fail. This option can be
useful in batch jobs and scripts where no user is present to enter a
password.
-W
--password
Force pg_restore to prompt for a password before connecting to a database.
The .pgpass file worked for me.
My setup:
I am restoring DB into the postgres instance running as Docker container.
The postgres instance is run using command:
docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6
pg_restore is present but it is available as separate tool, outside docker setup.
The ~/.pgpass file entry looks like this:
localhost:5432:db_name:user:password
Where:
db_name is the target db name that one is going to restore.
user is the name of the user that is going to perform the restore - in my case an admin user. i.e. postgres user in the postgres instance.
password - admin user's password. i.e. admin

Plink - Remote interactive connection with bash script does not seem to work properly

I have a redis database on a remote docker host, and I'd like to access it through a single ssh script command via plink.
The script is simple (redis-script.sh):
#!/bin/bash
echo "Enter Redis Password."
read -s pass
docker exec -it my-redis-container redis-cli -a $pass
Which works fine when I do a standard ssh connection via putty then run the script after login. I am able to enter the password and connect to the db:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379>
The problem is when I use plink, my plink command line:
plink.exe -t container-host /containers/redis-script.sh
I get this:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[6n
One issue is the mangled characters, but the biggest issue is that I can no longer type in any commands at this point. I am able to interact when it asks for the password, but once it gets into redis-cli, I cannot type anything.
Perhaps it's the docker exec command which is messing up the interactivity?
Any help is appreciated.

How to set environment variable and execute command in one line (PowerShell)?

How can I set value of environment variable and execute command in one line in PowerShell?
I have this:
PGPASSWORD=db_pass psql -U db_user -d db_name -a -h localhost -f some.sql
and it's working great in Linux.
How can I translate above to PowerShell command which will work on Windows Server 2012 - Windows 10?
I tried:
SET "PGPASSWORD=db_pass" & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql
and I get error: "The & is reserved for future use..." - Windows 2012.
Try this:
$env:PGPASSWORD='db_pass'; & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql
In reality this is two commands separated by a semi-colon so they can be run as a single line.
Note also that from PowerShell you set environment variables via $env:<name of var>.
just add a trailing command to the compound list:
;$env:PGPASSWORD=''
to get
"C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql; $env:PGPASSWORD=''
you might have to pipe add a sleep in there between the command and last reset of the variable, if the command is launched in asynchronous fashion otherwise you might end up resetting the variable before its use by the launched program. Mileage may vary.
It's a hack, but it works better than leaving the password in the environment forever.

Run postgres query from another server, in bash

I have a Ubuntu server that processes documents and another that have the database (postgresql 9.3).
I ran psql -? to understand how to connect to another DB and the final command would be:
psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
It works, but I must type the password after the command is issued.
I was trying to adapt this command in a bash script:
#!/bin/bash
psql -U postgres -W mypassword -h 1.2.3.4 -d testdb -p 5432 << EOF
select * from mytable;
\q
EOF
Needless to say this is not the right command.
Also, the password does not get recognized as a password, reporting the error:
psql: warning: extra command-line argument "mypassword" ignored
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
In another server, where the script runs on the local DB, my working script is:
su - postgres -c "psql myDatabase" << EOF
select * from "myOtherTable";
\q
EOF
The question is simple, how can I write the right command for bash, to connect to another database with user/password and issue commands?
A link I tried, but password seems to not be set:
run psql query in bash
Thanks!
Try
PGPASSWORD=yourpass psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
See: https://www.postgresql.org/docs/9.3/static/libpq-envars.html
or ~/.pgpass file https://www.postgresql.org/docs/9.3/static/libpq-pgpass.html

Resources