How to switch user in AIX shell script? - db2

I want to run a DB2 query from shell script. I have a user db2inst1 that's required to execute DB2 statements. I am running an application that uses root user to run the shell script. HERE if I switch user to db2inst1. I can do the job.
Following is my scipt that runs perfect with db2inst1.
#!/bin/sh
db2 "connect to customerdb"
db2 "set schema = db2inst1"
db2 "insert into tbl_customer(name,occupation) values ('Alex','Admin')"
See it like a root has to run it. So root user will let the script to switch user first before executing db commands.
My app will invoke this shell script which will use root user.
How can I switch user in script? I am new to shell scripting.

As you are running as root, you can use:
su dbinst1 -c './your_script arg1 arg2 ...'
Were you not running as root, life would be harder.

Related

How can I automate running an init script when invoking a postgres DB using psql?

psql (9.6.7, server 9.5.2) on linux
I have an init script... ~/sql.ini that I always want to run after connecting to a PG DB. I can do this by typing "\i ~/sql.ini", but is there a way to do this on the command line (which I alias) ?
Just put your commands in ~/.psqlrc file.
Your alias should call
PSQLRC=~/sql.ini psql
That environment variable specifies the startup file that psql executes automatically.

Automating Database Connection

For a homework, I have a few steps I have to go through every single time I want to connect to the database and it's becoming a really annoying and time-wasting act.
I've already automated part of it. However, my latest attempt at automating the last few commands hasn't been successful.
Initially, I've set up a shortcut to a PuTTy terminal:
Create new Shortcut
Select "C:\Program Files\PuTTY" as the entry point (Start in)
Enter "C:\Program Files\PuTTY\putty.exe" <MY_USERNAME>#arcade.iro.umontreal.ca -pw <MY_PASSWORD> as the Target
Then after double-clicking this shortcut, I entered these two lines (to create and then execute a bash script):
echo "psql -h postgres && \c ift2935 && set search_path to inscriptions_devoir;" > sql.sh
. sql.sh
Eventually, my goal would be to simply be able to write . sql.sh after opening my shortcut to be all set up and ready to go (and actually, maybe even that can be automatized somehow with the shortcut?). However, as it is, my shell script only runs the psql -h postgres command, which successfully launches PostGreSQL.
My question is:
How do I get the two other commands (\c ift2935 and set search_path to inscriptions_devoir;) to automatically run inside PostGreSQL?
EDIT:
Forgot to mention: after the first command of my script executes, I can then type \q to leave PostGreSQL and then the terminal outputs this:
-bash: c: command not found
Which, I think, indicates that the terminal interrupts its current process to actually run PostGreSQL and, on exit, it resumes the script, moving onto the second command, which fails because \c means nothing as a shell command.
While connected to the database, run:
ift2935=> ALTER ROLE <MY_USERNAME> SET search_path TO inscriptions_devoir;
This is your database user. Unless PGUSER is set, this should be the same as your operating system user, but you can always find it with SELECT current_user;.
Then the setting will automatically be active the next time you connect.
In your shell script, change the call to
psql -h postgres -d ift2935
Alternatively, and slightly better in my opinion, is the following, more complicated procedure:
Edit the file .bash_profile in your home directory and add
export PGHOST=postgres
export PGDATABASE=ift2935
Then disconnect and reconnect (this file is executed when you start a login shell).
Instead of running . sql.sh, simply type psql, which is less cumbersome.
Off topic: It is widely held that industriousness is the motor of progress. Nothing could be farther from the truth. Laziness is the mother of invention, specifically laziness paired with curiosity. If you plan to go into the computer engineering business, I promise you a bright future.
I think you should try using the pgpass file.
https://www.postgresql.org/docs/current/libpq-pgpass.html

Script not prompting for input variables when executing through root for oracle user

I am executing one shell script through root user, this script is further calling other script in oracle user. My pain is that i am unable to see prompt instruction for to put inputs, Could you please help.
Regards,

DB2 executing a Script in another Script

I am facing a problem in DB2. In my Oracle environment it was very easy for me to include multiple scripts in one master script, which were executed sequentially. e.g.:
Master.sql:
connect ....
#script1.sql
#script2.sql
Now I have to build up same logic in DB2 LUW. Is there a simple way to include multiple scripts in one master script? I would like to have one single db2 call from shell, which executes the master script and within all subscripts.
Regards
Jan
There is notrhing to stop you from creating a single file with multiple sql batches. In the Windows world, it would look like this:
Note: First you initialize the db2 command prompt.
db2cmd -c -w -i %1.bat
With as many of these as you want in the .bat file:
db2 -txf c:\Example\db2html.sql
In Linux, the db2clp is included in the shell once you load the db2profile ('. /home/db2inst1/sqllib/db2profile). In windows, you need to call db2cmd in order to use db2clp.
With a interactive db2clp, you cannot call db2 scripts via #scriptX, however, you can call them from the shell like
db2 -tvf script
However, if you use the CLP*Plus you can do almost everything you do in SQL*Plus. For more information: https://www.ibm.com/developerworks/community/blogs/IMSupport/entry/tech_tip_db2_s_new_clp_plus_utility?lang=en

how to run db2 sql command from a command line?

how can I run
sql command UPDATE CONTACT
SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'
via command line
for db2 database
on linux
from a shell script file?
You need to be logged into your linux machine with an id that has "db2profile" in it's .profile.
If you are not sure what I'm talking about, look at the .profile of the db2 instance owner (usually db2inst1 unless you changed it during the install). The simplest thing would probably be to log in as the db2 instance owner.
Once you are logged in, type "db2" at the command line. If "db2" is not found, then recheck the .profile stuff.
To execute your sql command type the following at the command line (substitute with the name of the database you want to connect to):
db2 connect to <database name>
db2 UPDATE CONTACT SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'