Run postgres query from another server, in bash - 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

Related

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

Running psql from shell always ask for connection details

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.

psql is not connecting to the localhost if listen_address is different

I am trying to fetch some data using psql command. If I run the command directly in the console its working fine. The same command if I try to run from shell script, it says unable to connect to psql server. I have checked the listen_address variable in postgresql.conf, as I expected it was set to some name instead of * or localhost.
This server is running under cluster environment, so its using virtual ip in listen_address. The pg_hba.conf allowing local (IPv4 and IPv6) with trust authentication.
It makes sense to me so far. But, I'm confused why it isn't allowing through shell script on same server?
Edit 1:
myuser # psql -d mydb -U postgres -c "select 1"
?column?
----------
1
(1 row)
myuser # cat myscript.sh
psql -d mydb -U postgres -c "select 1"
myuser # sh myscript.sh
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/run/postgresql/.s.PGSQL.5432"?

can't run scripts from cmd line windows for postgresql

I'm in a windows environemnt and kicked off psql. I've tried running -a -f filename.sql and psql filename.sql. This what I keep getting:
scala_db-# psql C:/Users/user/Desktop/emps.sql;
ERROR: syntax error at or near "psql"
LINE 1: psql C:/Users/user/Desktop/...
From the command line:
scala_db-# psql C:/Users/user/Desktop/emps.sql;
Looks like you already enter into the psql command line.
If so, you can use meta command \i to execute the SQL file.
e.g.
mydb=# \! echo "select 1" > c:/pgccc/tmp.sql
mydb=# \i c:/pgccc/tmp.sql
?column?
----------
1
(1 row)
If you hasn't logged into psql command line, you can use one single psql command line to execute the sql file.
e.g.
psql mydb -f c:/pgccc/tmp.sql
?column?
----------
1
(1 row)
Here the "mydb" is the target database. If not specified, you will connect to the default database.
When you run windows cmd
psql -U username dbname -f filename.sql
username is your user name,
dbname is your database name,
filename.sql is your file

Slimming down psql calls in database config creation

I currently have the following calls to psql to setup my PostgreSQL database. I'd like to slim this down as each time I have to input a password for every call. I haven't had much experience with psql so am not too sure about the best way to do all of the following in a shorter, more user friendly fashion.
Thanks.
# Create DB
psql -U postgres -h 127.0.0.1 -c "CREATE DATABASE \"main\" WITH OWNER maindb;"
# PostGIS setup
psql -U postgres --dbname main -h 127.0.0.1 -c "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;CREATE EXTENSION fuzzystrmatch;CREATE EXTENSION postgis_tiger_geocoder;"
# Create users table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Users.sql
# Create interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Interests.sql
# Create user-interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/UserInterests.sql
each time I have to input a password for every call
Solve this part by using a .pgpass file. Then you can make as many calls to psql as you want.

Resources