5

I want to write a shell script to run these commands. I usually connect from terminal using commands as below

    //first go to the directory 
     cd /opt/novell/sentinel/3rdparty/postgresql/bin/
    // then type following
     export LD_LIBRARY_PATH=/opt/novell/sentinel/3rdparty/postgresql/lib/
    // then fire following command 
     ./psql --host 127.0.0.1 --port 5432 --dbname=SIEM --username=dbauser
     Password for user dbauser: ****
2
  • So, you are OK with your password being stored in the shell script? Commented Jul 7, 2015 at 9:16
  • no i don't want to store password in my script Commented Jul 7, 2015 at 9:43

1 Answer 1

12

Why don't you update your PATH and export LD_LIBRARY_PATH permanently, by adding to your .profile these lines:

PATH=/opt/novell/sentinel/3rdparty/postgresql/bin/:$PATH
export LD_LIBRARY_PATH=/opt/novell/sentinel/3rdparty/postgresql/lib/

Then use the script to connect DB as simple as following

#!/bin/sh
psql --host=127.0.0.1 --port=5432 --dbname=SIEM --username=dbauser

After you run the script, you will be asked about the password.

If you would like not to enter password every time, you can use the password file .pgpass (see documentation for details), just add to your ~/.pgpass the following line:

127.0.0.1:5432:SIEM:dbauser:your_password

Be safe, dissallow any access to world or group:

chmod 0600 ~/.pgpass.

After this, you can connect to your db by using script above without password prompt.

Sign up to request clarification or add additional context in comments.

4 Comments

where do i create .pgpass file
@GauravRajput, .pgpass file usually is located in your home directory, or in a directory defined by PGPASSFILE environmental variable.
I had created this .pgpass file under root and then it worked fine.thank you very much
i get this error "/usr/bin/postgres : option invalide -- 'U'"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.