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 protected]'"
When i run the query UPDATE web_user SET password_hash ='$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2' WHERE email = '[email protected]';
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.
CodePudding user response:
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 protected]'"
output:
postgres=# select * from web_user;
pass | email
-------------------------------------------------------------- -----------------
$2a$12$ohJ0j2Y9lRkO6Ld9MaiLuu7Q4hzYSr1IsM5SfY1SxAGk6fgn20aj2 | [email protected]
(1 row)