Why does command module hang for ansible for psql? [duplicate] - postgresql

This question already has answers here:
creating jenkins jobs with ansible
(3 answers)
Ansible Command module says that '|' is illegal character
(2 answers)
Closed 4 years ago.
When I issue the psql command like this in my playbook:
- name: Run psql to pull in initial config data
become_method: sudo
become: yes
become_user: postgres
command: psql -U postgres -w eclaim < /opt/eclaim_revamp/sql_scripts/initial_config.sql
It takes forever to complete, looks as though hang, but when I use shell, it can get through:
- name: Run psql to pull in initial data
become_method: sudo
become: yes
become_user: postgres
shell: psql -U postgres -w eclaim < /opt/eclaim_revamp/sql_scripts/initial_sql_script.sql
Can anybody tell me why ?

From the documentation:
[The given command] will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work (use the shell module if you need these features).

Related

Docker Compose - Container Bash Forking

I am trying to run netbox based on their standard guide on Docker Hub with a slight difference that I need our existing postgres dump to be restored when the postgres container starts.
I have tried a few approaches like defining a command option in docker-compose file like (and a few more combinations):
sleep 2 && psql -U netbox -f netbox.sql
sleep is required to prevent psql command running before the postgres service is started.
Or defining a bash script that does the database restore but all these approaches cause the container to exit after that command/script is run.
My last resort was to utilize bash forking and this is what the postgres snippet of docker-compose looks like:
postgres:
image: postgres:13-alpine
env_file: env/postgres.env
command:
- sh
- -c
- (sleep 3 && cd /home && psql -U netbox -f netbox.sql) & su -c postgres postgres
volumes:
- ./my_db:/home/
- netbox-postgres-data:/var/lib/postgresql/data
Sadly this throws results in:
postgres: could not access the server configuration file
"/var/lib/postgresql/data/postgresql.conf": No such file or directory
If I omit the command section of docker-compose, the container starts up fine and I can navigate and ls the directory in the error message but it is not what I really need because this container will go on to be part of a much larger jungle of an ecosystem with little to no control over it afterwards.
Could it be my bash forking or the problem lies somewhere else?
Thanks in advance
I was able to find a solution by going through the thread that David Maze shared in the comments.
In my case, placing the *.sql file inside /docker-entrypoint-initdb.d did not work but I wrote a bash script, placed it in /docker-entrypoint-initdb.d directory and it got triggered.
The bash script was a very simple one, it would cd to the directory containing the sql dump and then restore it by running psql:
psql -U netbox -f netbox.sql

How to type text in CMD with batch file? [duplicate]

This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Postgresql: Scripting psql execution with password
(17 answers)
Run a PostgreSQL .sql file using command line arguments
(16 answers)
Closed 2 years ago.
Sorry if my question sounds a little bit obvious but I'm a little bit new on creating batch files, so the problem is the next one:
I'm creating a batch file that would to be open CMD and then go to a specific folder (psql) in order to execute some commands witch allow me to load data to a DataBase from a file (.csv)
This is my try:
cd "C:\pgsql\bin"
psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext#suggestedorder -c "\copy planning.prueba (centro, almacen, fecha_carga)from 'C:\Users\geradiaz.MODELO\Desktop\Envase\Selección_Envase\Inputs\No_Seleccionado\o.csv' with delimiter as ','
MyPassword
As I've said the first line goes to pgsql folder, the second line execute the command to copy the data to the data base and the last one (MyPassword) supposes to would be writing my password in the next line when CMD ask for it, but it seems like this last line does not work, when I execute the batch it just keeps the screen with the message "Password for User...".
Do you know guys with kind of commands do I need to execute this batch file?
There is no way to pass the password as an argument in psql, but you can try using the PGPASSWORD environment variable.
The script would be something like this:
#echo off
C:
cd \pgsql\bin
set PGPASSWORD=MyPassword
psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext#suggestedorder -c ....
For further info, see here: https://www.postgresql.org/docs/9.0/libpq-envars.html

Mongo --ssl on bash script

I'm writing a bash script where it connects to the mongodb in different ways and I'll run this script on various projects - some of them require --ssl connection and some of them don't. So, I wanted to know a way for me to maybe declare a variable on top which will turn on or off depending on whether the project needs --ssl connection.
ssl="--ssl" #how do I determine whether to turn this variable on or off depending on whether the project needs --ssl?
Example of where its used in bash script
`master_var=`mongo ${ssl_mode} --eval "db.isMaster.ismaster"`
Another example in the bash script where I connect to mongo:
mongo --quiet ${ssl_mode} ${name_db} <<EOF
#some commands
EOF
Edit: I want all of this to be done on the bash script itself.
You can use environment variables:
if [ -n "$MYSCRIPT_ENABLE_SSL" ]; then
ssl_mode="--ssl"
fi
And from where you call the script:
MYSCRIPT_ENABLE_SSL=1 ./myscript.sh
or
export MYSCRIPT_ENABLE_SSL=1
./myscript.sh

How to remove a postgresql user

I followed this tutorial and made a typo where I was supposed to create a user for my django apps to connect as;
I was supposed to run su - postgres -c "createuser www-data -P" but I ran su - postgres -c "createuser www-dtata -P".
I dont want to proceed until I remove that user, which I don't know the command for. I found and tried DROP USER after searching around, but the terminal returned -su: DROP: command not found.
Run sudo su - postgres -c "dropuser www-dtata"
You can use dropuser console tool (see https://www.postgresql.org/docs/current/static/app-dropuser.html):
su - postgres -c "dropuser www-dtata"
Or use DROP USER SQL query (see https://www.postgresql.org/docs/current/static/sql-dropuser.html):
sudo -u postgres psql -c 'DROP USER "www-dtata";'
These 2 approaches do the same thing. In SQL version, you also need to use double quotes around DB user name, due to - in it.
First run the command
sudo su
Enter the user password for root access.
Then run the below command
su - postgres -c "dropuser www-dtata"
No password will be prompted

previous username after doing sudo -u sbsh [duplicate]

This question already has answers here:
How do you find the original user through multiple sudo and su commands?
(10 answers)
Closed 6 years ago.
I am using the system as "user1" and I sudo as different user say "sudo -u user2 sbsh" and the execute a perl script.
Is there a way to get the "user1" in the script I am running ?
I used ENV{'USER'}. but it is giving "user2". 'SUDO_USER' is also not working.
Try $ENV{'SUDO_USER'}.
Example:
$ sudo perl -E 'say for $ENV{USER}, $ENV{SUDO_USER}'