Passing variable from shell script to psql - postgresql

#!/bin/sh
today=$(date +"%Y%m%d")
echo $today
#20220720
psql -h myhost -U sqladmin -d trades 8787 -c "select * from recalculate_value("$today")";
When executing psql, the $today is not getting interpreted correctly.
What is the proper way to pass the $today in the psql command line?
select * from recalculate_value('20220720') ;

Related

[Windows 10]Problem escaping dollar sign contained in tablename for /copy command in psql

I am trying to setup a shell script to insert data from .csv files into postgreSQL. As a first test I just tried rto do it in terminal command, and I am running into an issue because my table has a '$' in its name and I was not able to come up with the proper way to escape it so far.
My command look like this :
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
I have made several test, with this command, if tablename='test1' the command works, but if tablename='prefix&test1' then the command fails as terminal is trying to parse what is after '$' as a variable name
So for example :
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY schtest.prefix$name from 'FILEPATH' with delimiter ',' ;"
ERROR: relation "schtest.prefix" does not exist
I'm using Windows powershell.
In both POSIX-compatible shells such as bash and in PowerShell, $ is a metacharacter, which - in unquoted use or inside "..." - requires escaping in order to be interpreted verbatim (in order to be passed through to a command as-is).
However, the escape character differs between these two shells:
For POSIX-compatible shells it is \:
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl\$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
For PowerShell it is ` (the so-called backtick; see about_Special_Characters):
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl`$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
you can add escaped double quotes around the table name
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY
\"ts_dev.tbl$trm_secndry_readings\" from 'FILEPATH' with delimiter ',' ;"

Psql output to file with column alias starting with number

I am running psql from command line and sending output to a file. It is a simple select statement on a view, but I am getting a syntax error when I have a column alias that starts with a number.
I ran the query in PgAdmin and it works (which makes me believe that this is some sort of issue with psql). I also tried adding a '_' to the beginning of the alias and that allows it to go through.
works: 'abc as "_1abc"'
doesn't work: 'abc as "1abc"'
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c
"SELECT abc as "1abc" From example.view
This is the error I get:
ERROR: syntax error at or near "1"
It is a problem with nested double quotes. You need to escape the inner ones.
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c "SELECT abc as \"1abc\" From example.view"

Mongoexport shell script - Error in query

I wrote a shell script to create a data dump of records updated yesterday using mongoexport command.
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query="'{\"updated_at\":{\$gte:new Date(${yesterday}),\$lte:new Date(${today})}}'"
echo ${query}
mongoexport -h $HOST -d $DOC -c $COL_NAME -u $USER -p $PWD -q ${query} -o $fileName
After adding query, when I run the shell script I get below error in console
'{"updated_at":{$gte:new Date(1484287200000),$lte:new Date(1484373600000)}}'
too many positional arguments: [Date(1484287200000),$lte:new Date(1484373600000)}}']
try 'mongoexport --help' for more information
When I run this query in command line it works properly. Can someone pls let me know why is this error when ran in shell script?
This works in command line.
$mongoexport -h <<HOST>> -d <<DOC>> -c <<COL> -u <<UN>> -p <<PWD>> -q '{"updated_at":{"$gte":new Date(1484287200000),"$lte":new Date(1484373600000)}}'
There is a rule of thumb in bash: when you use a variable, always surround it with double quotes. There are exceptions, but they are rare.
mongoexport -h "$HOST" -d "$DOC" -c "$COL_NAME" -u "$USER" -p "$PWD" -q "${query}" -o "$fileName"
This code worked for me
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query1="{\"transactionDate\":{\$gte: new Date(${yesterday}),\$lte: new Date(${today})}}"
echo $yesterday
echo $today
mongoexport -d databasename-c collectionname --host yourip --port 27017 -p password -u username-q "${query1}" --type=csv --fields=transactionDate,amount > test5.csv

How to return a value from psql to bash and use it?

Suppose I created a sequence in postgresql:
CREATE SEQUENCE my_seq;
I store the below line in an sql file get_seq.sql
SELECT last_value FROM my_seq;
$SUDO psql -q -d database_bame -f get_seq.sql
How do I get the int number returned by SELECT into bash and use it?
You can capture the result of a command using the VAR=$(command) syntax:
VALUE=$(psql -qtAX -d database_name -f get_seq.sql)
echo $VALUE
The required psql options mean:
-t only tuple
-A output not unaligned
-q quiet
-X Don't run .psqlrc file
Try:
LAST_VALUE=`echo "SELECT last_value FROM my_seq;" | psql -qAt -d database_name`

Calling psql pg_dump command in a batch script

I am trying to call a pg_dump command in a batch file. First I get all the table names and then loop every table and execute pg_dump command. It has to be probably something like that but I get an error as "syntax error":
for %%T in (psql -U postgres -w -d test_db -t -c "SELECT table_name FROM
information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'")
do pg_dump -t %%T -U postgres test_db -w -f "C:\Users\mtuna\Documents\dumpfiles\%%T.sql"
done;
Any help would be appreciated.
Here is a solution:
#echo off
SET TableListeFile=C:\Users\mtuna\Documents\dumpfiles\database_list.txt
REM Saveing all tables name of database test_db on a temp file: database_list.txt
psql -U postgres -d test_db -t -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'" -o "%TableListeFile%"
REM Loop on liste tables name:
FOR /F "tokens=*" %%I IN (%TableListeFile%) DO (
REM Dump each table on file
pg_dump -U postgres -h localhost -t %%I test_db > "C:\Users\mtuna\Documents\dumpfiles\%%I.sql"
)
REM Delete temp file
del /Q %TableListeFile%
It will prompt you for password input for every dump. If you don't want to be promted, you can use the Pgpass File.
Hope that helps.
Houari.
Backup: batch_file_backup.bat
#echo off
SET PGPATH="E:\PostgreSQL\9.5\bin\pg_dump.exe"
SET PGPASSWORD=admin
%PGPATH% -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup database_name
Bat File Backup Run PHP Code:
$batchfile_path = "E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_backup.bat";
$WshShell = new COM("WScript.Shell");
$exec = $WshShell->Run($batchfile_path, 0, false);
Restore:batch_file_restore.bat
#echo off
SET PGPATH="E:\PostgreSQL\9.5\bin\pg_restore.exe"
SET PGPASSWORD=admin
%PGPATH% -h 127.0.0.1 -p 5432 -U postgres -d database_name -v C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup
Bat File Restore Run PHP Code:
$batchfile_path =
E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_restore.bat";
$WshShell = new COM("WScript.Shell");
$exec = $WshShell->Run($batchfile_path, 0, false);
References: http://www.somelesson.blogspot.com/2017/04/postgresql-backup-and-restore.html