我要评分
获取效率
正确性
完整性
易理解

(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.

  1. Switch to user postgres.
    su - postgres
  2. 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
  3. 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.
    1. Log in to the database.
      /usr/local/pgsql-13.2/bin/psql -U postgres
    2. Set the password of the database user postgres.
      alter user postgres with password '123456';
    3. Exit the database.
      \q
  4. 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.

    1. Modify the pg_hba.conf file to allow all users to remotely manage all databases.
      1. Open the file.
        vi /data/pg-13.2/data/pg_hba.conf
      2. 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

      3. Press Esc, type :wq!, and press Enter to save the file and exit.
    2. Modify the postgresql.conf file to enable functions such as remote connection and replication.
      1. Open the postgresql.conf file.
        vi /data/pg-13.2/data/postgresql.conf
      2. 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.

      3. Press Esc, type :wq!, and press Enter to save the file and exit.
    3. 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