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

Running PostgreSQL

After installing PostgreSQL, you can initialize, start, and log in to the database, configure the database user account and password, and modify the configuration file to check whether the database is running properly.

  1. Initialize the database.

    Perform this step as the postgres user.

    1. Switch to the postgres user.
      1
      su - postgres
      
    2. Open the environment variable file.
      1
      vim .bash_profile
      
    3. Press i to enter the insert mode and add the public library path at the bottom of the environment variable file.
      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgsql/lib

    4. Press Esc, type :wq!, and press Enter to save the file and exit.
    5. Make the modification take effect.
      1
      source .bash_profile
      
    6. Initialize the database.
      1
      /usr/local/pgsql/bin/initdb -D /data/pgsql
      

  2. Start the database.
    1. Start the PostgreSQL database.
      1
      /usr/local/pgsql/bin/pg_ctl -D /data/pgsql -l logfile start
      

    2. Check whether the PostgreSQL database process is started properly.
      1
      ps -ef | grep postgres
      

      As shown in the preceding figure, related PostgreSQL processes have been started.

  3. Log in to the database.
    1
    /usr/local/pgsql/bin/psql -U postgres
    

    You do not have to enter the password for the first login.

  4. Change the password of the database account.

    After login, set the password of the postgres user.

    1
    alter user postgres with password '123456';
    

  5. Exit the database.
    1
    \q
    

  6. Stop the database.
    1
    /usr/local/pgsql/bin/pg_ctl -D /data/pgsql -l logfile stop
    

  7. Modify the database configuration file.
    1. Open /data /pg_hba.conf.
      1
      vim /data/pgsql/pg_hba.conf
      
    2. Press i to enter the insert mode and add the following content to allow non-local access. Replace the example IP address with the actual one.
      host all all 192.168.202.0/24 trust

    3. Press Esc, type :wq!, and press Enter to save the file and exit.
    4. If the database service is started, run the following command to reload the configuration file after the configuration is complete: If the database service is stopped, restart the database service for the modification to take effect.
      1
      /usr/local/pgsql/bin/pg_ctl reload -D /data/pgsql