(Optional) Initializing and Starting PostgreSQL
Initialize and start the PostgreSQL database as user postgres, set the user password, and configure remote management. If the PostgreSQL primary/secondary deployment environment has been prepared, skip this section.
- Switch to user postgres.
su - postgres
- Initialize and start the database as the postgres user.
rm -rf /data/pg-13.2/data/* /usr/local/pgsql-13.2/bin/initdb -D /data/pg-13.2/data /usr/local/pgsql-13.2/bin/pg_ctl -D /data/pg-13.2/data -l logfile start
- Set the password of the database user postgres. This document uses the password 123456 as an example. For security purposes, please use a more complex and secure password in the production environment.
- Log in to the database.
/usr/local/pgsql-13.2/bin/psql -U postgres
- Set the password of the database user postgres.
alter user postgres with password '123456';
- Exit the database.
\q
- Log in to the database.
- Configure remote management.
Modify the pg_hba.conf and postgresql.conf files to allow users to remotely manage the database through password authentication from any IP address.
- Modify the pg_hba.conf file to allow all users to remotely manage all databases.
- Open the file.
vi /data/pg-13.2/data/pg_hba.conf
- Press i to enter the insert mode and add the following content to the end of the file:
host all all 0.0.0.0/0 password

- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Modify the postgresql.conf file to enable functions such as remote connection and replication.
- Open the postgresql.conf file.
vi /data/pg-13.2/data/postgresql.conf
- Press i to enter the insert mode and add the following settings to the end of the file:
listen_addresses = '*' # This parameter specifies the IP address to be listened by the PostgreSQL service. If set to *, the server listens to all available IP addresses, including IPv4 and IPv6 addresses, and can receive connections from any client IP address connected to the server. port = 5432 # This parameter specifies the listening port number of the PostgreSQL service, with the default value of 5432. wal_level = replica wal_keep_size = 1000 max_slot_wal_keep_size = 10 wal_sender_timeout = 120s max_wal_senders = 10 min_wal_size = 800MB archive_mode = on archive_command = 'cp %p /data/pg-13.2/archive/%f' hot_standby = on max_standby_streaming_delay = 30s wal_receiver_status_interval = 10s hot_standby_feedback = on max_connections = 200 # This parameter specifies the maximum number of concurrent connections allowed by the database server. wal_log_hints = on

Check the description of other parameters in the PostgreSQL configuration file on the PostgreSQL official website.
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the postgresql.conf file.
- Run the following command to restart the database for the configurations to take effect:
/usr/local/pgsql-13.2/bin/pg_ctl -D /data/pg-13.2/data -l logfile restart
- Modify the pg_hba.conf file to allow all users to remotely manage all databases.
Parent topic: Deployment Environment