How to move default location of postgresql.conf file to another in PostgreSQL?

In this article, we will see how to move default location of postgresql.conf file to another in PostgreSQL?

Why we need to modify the default location of PostgreSQL.conf file?

1. By organization rule.
2. Or Maintaining standard location
3. Or Put all configuration files and scripts in the directory that is frequently backued up.

Steps to move default location of postgresql.con file to another in PostgreSQL

To modify the default location of postgresql.conf, its important to have safe location that can be accessed by a postgres user.
It is also important to backup configuration file if it is not part of data directory.

1. We have to stop the PostgreSQL cluster before modify by using below command.

pg_ctl -D $PGDATA stop -mf

How to move default location of postgresql.con file to another in PostgreSQL

2. Create the directory and assign ownership and access to postgres user using below commands.

sudo mkdir -p /pgconfigs sudo chown postgres:postgres /pgconfigs/ sudo chmod -R 700 /pgconfigs/

3. Now move the original postgresql.conf file to new location.

mv $PGDATA/postgresql.conf /pgconfigs/postgresql.conf

or

mv /var/lib/pgsql/14/data/postgresql.conf /pgconfigs/postgresql.conf

4. Now start the PostgreSQL server that was stopped in the step 1 using new location.

pg_ctl -D $PGDATA -o '--config-file=/pgconfigs/postgresql.conf'

5. Now verify the location of configuration files using show command.

psql -c "SELECT show_config"