Query working fine on PGAdmin but not on Terminal - sql

I am planning to create a cronjob that will change the password of my site every 15th day of the month. This is my .sh file
psql -U postgres -h (sample-host) (sample-db) -p (sample-port) -c "UPDATE web_user SET password_hash = '$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2' WHERE email = 'email#email.com'"
When i run the query UPDATE web_user SET password_hash ='$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2' WHERE email = 'email#email.com'; on pgadmin, everything is fine. The update is fine and the password is right. But when I run my .sh file on my machine(ubuntu 18.04), and even manually running it on the terminal, the result will be just a.
There are no errors messages or anything like that. Is there something that I missed? BTW the version of is postgresql 13.4.
Update: I just found out that special characters are causing the problem. it seems that the psql command does not allow special characters. The problem is I can't find any resources about these things.

For the special characters you need to a workaround
I tried in powershell, may be work in linux terminal
before:
postgres=# select * from web_user;
pass | email
------+-------
(0 rows)
used echo to output the password_hash as it contains special characters.
psql -U postgres -h localhost -d postgres -p 5432 -c "UPDATE web_user SET pass = '$(echo '$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2')' WHERE email = 'email#email.com'"
output:
postgres=# select * from web_user;
pass | email
--------------------------------------------------------------+-----------------
$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2 | email#email.com
(1 row)

Related

Execute an Impala query and get query time

I want to be able to execute a number of Impala queries and return the time it took for each query to execute. Using the Impala shell, I can do this with the following command:
impl -q "select count(*) from database.table;"
This gives me the output
Using service name 'impala'
SSL is enabled. Impala server certificates will NOT be verified (set --ca_cert to change)
Connected to *****.************:21000
Server version: impalad version 2.6.0-cdh5.8.3 RELEASE (build c644f476b774db9db87a619628f7a6ecc5f843e0)
Query: select count(*) from database.table
+----------+
| count(*) |
+----------+
| 1130976 |
+----------+
Fetched 1 row(s) in 0.86s
I want to be able to fetch that last line and extract the time. It doesn't really matter how, which is why I haven't tagged a language. I have tried using grep like this:
impl -q "select count(*) from database.table" | grep -Po "\d+\.\d+"
But that does nothing but remove the table. Putting the query in a python script and using subprocess couldn't find impl as a command, and same for scala.
The weird thing is that impala-shell dumps those messages to stderr rather than to stdout, so to fetch the last line, you would have to append a 2>&1 to redirect stderr to stdout
impala-shell -q "query string" 2>&1 | grep -Po "\d+\.\d+(?=s)"
Notice that a positive lookahead (?=s) is probably required to avoid capturing version numbers

Batch file: escape a % character into a PostgreSQL query

I need to use a % character into a SQL query that I pass into a windows batch file. My issue is that this character is not taken into account.
towns=(12232, 12233)
for %%a in %towns% do (
"C:\Program Files\PostgreSQL\9.4\bin\pgsql2shp.exe" -h localhost -u thomas -P password thomas_db "SELECT * FROM schema.table WHERE code like '%%a%'"
)
I get the following error:
C:\batch><"C:\Program Files\PostgreSQL\9.4\bin\pgsql2shp.exe" -h localhost -u thomas -P password thomas_db "SELECT * FROM schema.table WHERE code like '12232'">
Initializing...
ERROR: Could not determine table metadata
The LIKE '%%a%' is interpreted as LIKE '12232' and should be as LIKE '12232%'.
A double percent escapes a percent so double it up:
... WHERE code like '%%a%%%'"

How do I get a user input and apply it in a sql statement in bash?

I have two scripts. One is named sqlscript.sql and the other is named script.sh I have all of the queries needed written in my sql script. They are just a bunch of update statements. For example:
UPDATE xxDev.SYS_PARAMS SET val = 'serverName' WHERE lower(name) = 'enginebaseurl';
I'm running the .sql script IN the .sh script. When the .sh script runs, I want it to prompt the user for a server name and take that user input and replace it in serverName in the sql statements.
I'm brand new to both bash scripting and this website, so I hope I'm making sense asking this question. I'm using PuTTY if that makes a difference at all.
Suppose you use MySQL, try something like:
# TODO: prompt user for server name and store it into variable serverName
serverName="get from user"
cat <<"EOF" | mysql -u user1 -p passwd -h server1 -P 3306 -D db1
UPDATE xxDev.SYS_PARAMS SET val = '$serverName' WHERE lower(name) = 'enginebaseurl';
EOF
So in this example, you embed the sql script into the .sh so that you don't have to maintain two files.
I would probably use a variable
set #val 'serverName'
UPDATE xxDev.SYS_PARAMS SET val = #val WHERE lower(name) = 'enginebaseurl';
You can split the sqlscript.sql into
set-val.sql
set #val 'serverName'
and the actual update statements. Then you can recreate the set-val.sql from your user input:
echo -n "enter server: "
read server
echo "set #val '$server' > set-val.sql
and then you forward both files to mysql:
cat set-val.sql sqlscript.sql | mysql
You should probably use this only for internal things, it seems a little fragile.
I'm going let you figure out how to pass a shell parameter into your sql command, but here's an incredibly cool way to query the user for the server name. It might even be POSIX compliant.
#!/bin/sh
echo -n "Hit me with that server name: "; read serverName
echo "${serverName}! Outstanding! Pick up \$200 when you pass Go!"

Running oracle script as oracle user from a shell script that runs as root

I have a shell script that runs as root. I want the script to switch to oracle user, run sqlplus and run some .sql files.
I am trying to followung :
su - oracle << -EOF1 2>&1
sqlplus $user/$password << -EOF2
#oracle.sql;
#quartz.sql;
EOF2
EOF1
first of all i get stty: standard input: Inappropriate ioctl for device what does it mean ?
second, can someone explain to me how the redirect (should) work in this case ?
Thanks
Use:
if [ "$(id -un)" -eq "root" ]; then
exec su - oracle -c $0
fi
sqlplus <<EOF
blablabla
EOF
If your script potentially takes arguments, the solution will differ.
What this does it checking whether the user running is currently root. If so, it re-executes the script ($0) as user oracle instead.
But BTW, why does the script run as root in the first place?
su - oracle -c " echo 'select 1 from dual;
select 2 from dual;'| sqlpus / as sysdba "
if contain ' using following
su - oracle -c " echo \"select 1 from dual;
select 2 from dual;\" | sqlpus / as sysdba "

Msg 102, Level 15, state 1 Line 1 Incorrect syntax near 'n'

I have the following query which I am running using a batch file. In the batch file I use the following syntax:
echo populating Application table with values...
SET "installFile=%sqlDir%\Install\DataFiles\Insert_ApplicationNames.sql"
OSQL /n /i "%installFile%" /d%db% /U%user% /P%pswd% /S%serv%
echo
echo populated Application table with values in Insert_ApplicationNames.sql
echo
The sql shown below runs without any errors when executed from the SQL Management Studio, but it keeps erroring out when run as a part of the batch script. Could some one help me find what I may be doing wrong here?
Also, the rows do get inserted, but our nightly QA install breaks because of the error thrown by the batch script.
IF NOT EXISTS(SELECT * FROM Application WHERE name = '')
BEGIN
INSERT INTO Application
(Name)
VALUES
('')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App1.exe')
BEGIN
INSERT INTO Application
(Name)
VALUES
('App1.exe')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App2.exe')
BEGIN
INSERT INTO Application
(Name)
VALUES
('App2.exe')
END
GO is the (default) batch separator keyword in Management Studio, but it isn't a real SQL keyword (i.e., SQL Server doesn't recognize it).
Remove those from your script -- in the script you've provided, they are irrelevant anyway -- and you should be good to, um, go.
Curious whether your variables should be right up against the switches. Try this?
OSQL -n -i "%installFile%" -d %db% -U %user% -P %pswd% -S %serv%
What happens when you use the line above with your known good values right in the command?
OSQL -n -i "C:\foo.sql" -d MyDB -U MyUser -P MyPwd -S MyServ