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:
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/pg_ctl –D /u01/data start |
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.
1 2 3 4 5 6 7 8 |
[postgres@postgres1 ~]$ pg_ctl start waiting for server to start....2023-06-12 15:42:10.622 IST [3648] LOG: listening on IPv6 address "::1", port 5432 2023-06-12 15:42:10.623 IST [3648] LOG: listening on IPv4 address "127.0.0.1", port 5432 2023-06-12 15:42:10.628 IST [3648] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2023-06-12 15:42:10.648 IST [3649] LOG: database system was shut down at 2023-06-07 19:25:03 IST 2023-06-12 15:42:10.655 IST [3648] LOG: database system is ready to accept connections done server started |
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:
1 |
[postgres@postgres1 ~]$ vi .bash_profile |
2. Add the export statements for desired environmental variables. These are some commonly used environmental variables.
1 2 3 4 5 6 |
PATH=$PATH:$HOME/bin #export PATH=/usr/local/pgsql/bin/:$PATH #export PGDATA=/u01/data1 #export PGDATABASE=postgres #export PGUSER=postgres #export PGPORT=5432 |
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:
1 |
[postgres@postgres1 ~]$ . .bash_profile |
5. Now you are ready to use the variables, here are some examples:
1 2 3 4 5 6 |
[postgres@postgres1 ~]$ pg_ctl start server started [postgres@postgres1 ~]$ psql psql (11.0) [postgres@postgres1 ~]$ pg_ctl stop server stopped |
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.
1 |
[postgres@postgres1 ~]$ vi pg_env1.sh |
2. Add export statements for the desired environmental variables to the file.
1 2 3 4 5 6 |
PATH=$PATH:$HOME/bin export PATH=/usr/local/pgsql/bin/:$PATH export PGDATA=/u01/data1 export PGDATABASE=postgres export PGUSER=postgres export PGPORT=5432 |
3. Save the file and exit by using command ‘:wq!’.
4. To set the environmental variables run the following command:
1 |
[postgres@postgres1 ~]$ source pg_env1.sh |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[postgres@postgres1 ~]$ pg_ctl stop -m i 2023-06-12 17:30:44.723 IST [4410] LOG: received immediate shutdown request. waiting for server to shut down....2023-06-12 17:30:44.728 IST [4415] WARNING: terminating connection because of crash of another server process. 2023-06-12 17:30:44.728 IST [4415] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory. 2023-06-12 17:30:44.728 IST [4415] HINT: In a moment you should be able to reconnect to the database and repeat your command. 2023-06-12 17:30:44.732 IST [4410] LOG: database system is shutdown. done server stopped [postgres@postgres1 ~]$ pg_ctl start waiting for server to start....2023-06-12 17:30:59.747 IST [5057] LOG: listening on IPv6 address "::1", port 5432 2023-06-12 17:30:59.747 IST [5057] LOG: listening on IPv4 address "127.0.0.1", port 5432 2023-06-12 17:30:59.755 IST [5057] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2023-06-12 17:30:59.774 IST [5058] LOG: database system was interrupted; last known up at 2023-06-12 16:46:12 IST 2023-06-12 17:31:01.335 IST [5058] LOG: database system was not properly shut down; automatic recovery in progress 2023-06-12 17:31:01.340 IST [5058] LOG: redo starts at 0/164C958 2023-06-12 17:31:01.340 IST [5058] LOG: invalid record length at 0/164CA38: wanted 24, got 0 2023-06-12 17:31:01.340 IST [5058] LOG: redo done at 0/164CA00 2023-06-12 17:31:01.361 IST [5057] LOG: database system is ready to accept connections done server started |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[postgres@postgres1 ~]$ pg_ctl stop -m f 2023-06-12 17:31:16.341 IST [5057] LOG: received fast shutdown request waiting for server to shut down....2023-06-12 17:31:16.345 IST [5057] LOG: aborting any active transactions 2023-06-12 17:31:16.346 IST [5057] LOG: background worker "logical replication launcher" (PID 5064) exited with exit code 1 2023-06-12 17:31:16.346 IST [5059] LOG: shutting down 2023-06-12 17:31:16.373 IST [5057] LOG: database system is shut down done server stopped [postgres@postgres1 ~]$ pg_ctl start waiting for server to start....2023-06-12 17:31:23.566 IST [5068] LOG: listening on IPv6 address "::1", port 5432 2023-06-12 17:31:23.566 IST [5068] LOG: listening on IPv4 address "127.0.0.1", port 5432 2023-06-12 17:31:23.573 IST [5068] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2023-06-12 17:31:23.597 IST [5069] LOG: database system was shut down at 2023-06-12 17:31:16 IST 2023-06-12 17:31:23.601 IST [5068] LOG: database system is ready to accept connections done server started |
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.
1 2 3 4 5 6 7 |
[postgres@postgres1 ~]$ pg_ctl stop -m s 2023-06-12 17:31:30.172 IST [5068] LOG: received smart shutdown request waiting for server to shut down....2023-06-12 17:31:30.175 IST [5068] LOG: background worker "logical replication launcher" (PID 5075) exited with exit code 1 2023-06-12 17:31:30.179 IST [5070] LOG: shutting down 2023-06-12 17:31:30.203 IST [5068] LOG: database system is shut down done server stopped |
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
Note: Please test scripts in Non Prod before trying in Production.