previous username after doing sudo -u sbsh [duplicate] - perl

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}'

Related

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

put automatically password of user in pg_restore command line windows [duplicate]

This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Closed 4 years ago.
I want to restore my database postgres using pg_restore in command line and i want to put the password of user directly in the command, I tried this command but doesn't work
pg_restore -h localhost -p 5432 -U postgres -W 123 -d my_db my_backup.backup
but the following message is displayed
pg_restore: too many arguments on the command line (the first being "-d")
Set the password as an environment variable: set "PGPASSWORD=123"
You can set other arguments this way as well: https://www.postgresql.org/docs/current/static/libpq-envars.html
-W is to force a password prompt, not to supply a password directly, thats why it says there are too many arguments.
Putting it all together:
set "PGPASSWORD=123"
pg_restore -h localhost -p 5432 -U postgres -d my_db my_backup.backup
Update: Thanks #aschipfl, I initially had incorrect quoting on set

pg_dumpall without prompting password [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)
Run batch file with psql command without password
(4 answers)
Closed 4 years ago.
We are trying to automate the backup of all PostgreSQL databases of a cluster, and we decided to use 'pg_dumpall' utility. But we couldn't find a way to execute 'pg_dumpall' without prompting password. We are using PostgreSQL-10.
We are trying to execute the following command
pg_dumpall -U "username" -h "hostname" > "location"
How can we automate pg_dumpall without prompting password? Please help us on this.
Thanks
I am answering my own question
we can use ".pgpass" file to avoid prompting password
" CREATING AND USING A .PGPASS FILE
=====================================
A .pgpass file will allow you to use postgres CLI tools such as psql and pg_dump without having to manually enter a password — You can use the programs from scripts without having to run them as a non password protected user.
First, create the .pgpass file
#nano /root/.pgpass
According to the official doccumentation the format of the file is as follows:
hostname:port:database:username:password
The file supports the use of # tags for comments and * to match on wildcards. Here is an example of mine:
*:*:*:postgres:jerry_pass
Enter your database information and save.
Next, set the permissions. If they are not 600 Postgres will ignore the file.
#chmod 600 /root/.pgpass
Now how do we make postgres use it? A quick scan through psql man page shows:
-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.
Here are some usage examples:
#psql -d postgres -U postgres -w
#pg_dump -U postgres -w -Fc "

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

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).

import sql dump into postgres [duplicate]

This question already has answers here:
Import SQL dump into postgresql [closed]
(3 answers)
Closed 10 years ago.
when i run this command on ubuntu terminal:
psql -h localhost -U arwinder --password -f sh_cake_db_14_02_13.sql -d new_db
where "arwinder" is user name and "sh_cake_db_14_02_13.sql" is sql dump and "new_db" is database.
then while importing it shows following errors and in postgresql database I don't have all the tables. Tese are the errors:
invalid command \N
invalid command \
invalid command \
ERROR: syntax error at or near "27"
LINE 1: 27 Balaji Angan
There is a convert tool https://github.com/lanyrd/mysql-postgresql-converter ,or you can refer to postgressql's wiki http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL , see MySQL section and check your data types.