How to set PostgreSQL environmental variables permanently in Ubuntu Linux

In this article, we will see how to set PostgreSQL environmental variables permanently in Ubuntu Linux step by step.

How to set PostgreSQL environmental variables

Setting environment variables are useful when performing tasks within PostgreSQL including starting and shutting down the postmaster processes. In this article, we are going to set following environmental variables for PostgreSQL.

1. bin
2. data directory
3. postgres database
4. postgres user
5. port

Examples we are going to show are for PostgreSQL version 14 on Ubuntu. For other PostgreSQL version just replace the version number.

1. Set bin

echo "PATH=/usr/lib/postgresql/14/bin" >> ~/.bash_profile

2. Set Data directory in PostgreSQL

echo "PGDATA=/etc/postgresql/14/main" >> .bash_profile

3. Set postgres database.

echo "PGDATABASE=postgres" >> ~/.bash_profile

4. Set postgres user.

echo "PGUSER=postgres" >> ~/.bash_profile

5. Set postgres port.

echo "PGPORT=5432" >> ~/.bash_profile

Finally run the source command.

source ~/.bash_profile

To verify issue the command cat .bash_profile or

So in this article, we have seen how to set environment variables for PostgreSQL in Ubuntu Linux distribution successfully.