ENVIRONMENT VARIABLES IN PostgreSQL:

Share via:

ENVIRONMENT VARIABLES IN PostgreSQL:

These are useful to avoid hard-coding database connection information into simple client applications, for example.
To start a PostgreSQL server, you can run the following command:

However, you can also use an environmental variable to create a shortcut for this command. By setting up the variable, you can simply use ‘pg_ctl’ start, to start the server.

There two methods which is used to setup environmental variables in PostgreSQL:
1, Modifying the ‘.bash_profile’
2. Creating a custom script file ‘pg_env.sh’
In case of single cluster it is preferred to use ‘.bash_profile’
Setting up .bash_profile :
1. Open the .bash_profile file by running the following command:

2. Add the export statements for desired environmental variables. These are some commonly used environmental variables.

3. Save & exit the file by using the command ‘:wq!’ in the vi editor.
4. To apply the changes to the prompt run the following command:

5. Now you are ready to use the variables, here are some examples:

With this method, the environmental variables will be automatically set every time you start a new shell session. This allows you to use the variables without manually setting them each time,
In case of multiple clusters, it is preferred to use custom scripts like “pg_env.sh”.
Setting up custom scripts “pg_env,sh”:
1. Create a new file “pg_env1.sh” using a text editor.

2. Add export statements for the desired environmental variables to the file.

3. Save the file and exit by using command ‘:wq!’.
4. To set the environmental variables run the following command:

To manage multiple clusters, you can create custom scripts with different filenames like ‘pg_env1.sh’ and ‘pg_env2.sh’. Whenever you need to use a specific cluster, run the source command for the corresponding script as mentioned above.
Note: Each time you start a new shell session, you should run the source command to set the environmental variables for the desired cluster.
SHUTDOWN MODES:
There are 3 types of shutdown modes in PostgreSQL.
1. Immediate shutdown
2. Fast shutdown
3. Smart shutdown
Immediate Shutdown:
The “immediate” shutdown mode is similar to “fast” mode but skips some additional cleanup steps. It forcefully terminates all client connections, aborts active transactions, and shuts down the server without performing some necessary shutdown procedures. This mode is useful when you need to shut down the server urgently and are willing to take the risk of potential recovery issues.

Fast shutdown:
The “fast” shutdown mode is useful when you need to stop the server quickly, and data loss is acceptable. It forcefully terminates all client connections and aborts any active transactions. This mode is suitable when you want a rapid shutdown and are willing to sacrifice any uncommitted changes.

Smart shutdown:
The “smart” shutdown mode is a default shutdown mode. It allows the server to stop by waiting for all active transactions to complete before shutting down. This mode ensures that data integrity is maintained and that all changes are durably stored on disk.

Author    : Prudhvi Teja

LinkedIn  : http://linkedin.com/in/prudhvi-teja-nagabhyru-715052224

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment