PostgreSQL .bash_profile Set Up
PostgreSQL .bashprofile is used to access the postgresql utility anywhere in the server it contains Postgresql environment variable like username, password, port, data directory, database, bin path ..etc. It’s a shell script you can run it by using dot symbol (.),
Before accessing the postgresql utilities you have to run it first.
From following examples you will understand it clearly.
Step 1:Go to pg_env file and check the environment variable
[root@r1 ~]# cd /opt/PostgreSQL/9.3/
3rd_party_licenses.txt installer/ scripts/
bin/ lib/ share/
data/ license.txt stackbuilder/
doc/ pgAdmin3/ uninstall-postgresql
include/ pg_env.sh uninstall-postgresql.dat
[root@r1 ~]# cat /opt/PostgreSQL/9.3/pg_env.sh
#!/bin/sh
# The script sets environment variables helpful for PostgreSQL
export PATH=/opt/PostgreSQL/9.3/bin:$PATH
export PGDATA=/opt/PostgreSQL/9.3/data
export PGDATABASE=postgres
export PGUSER=postgres
export PGPORT=5432
export PGLOCALEDIR=/opt/PostgreSQL/9.3/share/locale
export MANPATH=$MANPATH:/opt/PostgreSQL/9.3/share/man
Step 2:Copy the “pg_env” file and paste into the “.bash_profile” file if your server data is different partition means just change the data directory path as per your server setup
[postgres@r1 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export PATH=/opt/PostgreSQL/9.3/bin:$PATH
export PGDATA=/opt/PostgreSQL/9.3/data
export PGDATABASE=postgres
export PGUSER=postgres
export PGPORT=5432
export PGLOCALEDIR=/opt/PostgreSQL/9.3/share/locale
export MANPATH=$MANPATH:/opt/PostgreSQL/9.3/share/man
Step 3. if you not setup bash_profile means you have to access the postgres utility after went to postgresql bin path like following :
[postgres@r1 ~]$ cd /opt/PostgreSQL/9.3/bin/
[postgres@r1 bin]$./psql -p 5432 -d postgres -U postgres
Password for user postgres:
psql.bin (9.3.14)
Type "help" for help.
No entry for terminal type "xterm";
using dumb terminal settings.
postgres=#
Step 4:Run the Bash Profile as “Postgres user”
[postgres@r1 ~]$ . .bash_profile
Step 5:Then you can easily connect the Postgres server using with “psql” Utility without going to bin utility path
[postgres@r1 ~]$ psql
Password:
psql.bin (9.3.14)
Type "help" for help.
No entry for terminal type "xterm";
using dumb terminal settings.
postgres=#
Step 6:you can call the linux postgresql utility without going postgresql bin path if you not set bash_profile and want to take backup means first you need to go to bin path(/opt/PostgreSQL/9.3/bin) then you have to issue
./pg_dump
Running bash_profile:
[postgres@r1 ~]$ . .bash_profile
[postgres@r1 ~]$ pwd
/home/postgres
calling createdb utility from our home path:
[postgres@r1 ~]$createdb johndb
Password:
[postgres@r1 ~]$
calling psql utility :
[postgres@r1 ~]$psql johndb
Password:
psql (9.1.2.2)
Type "help" for help.