PostgreSQL is used in a recent project and needs to be installed on the server. Record the installation process for future reference.

First, environmental preparation

The PostgreSQL version is 11.4, Centos is 7.6, and nothing else.

Two, deployment and installation

To configure the required environment, run the following command:

    $ yum install -y vim lrzsz tree wget gcc gcc-c++ readline-devel
Copy the code

To create a directory and enter the specified directory, run the following command:

$ mkdir /opt/postgresql
$ cd /opt/postgresql
Copy the code

To download the PostgreSQL installation package, run the following command:

$wget HTTP: / / https://ftp.postgresql.org/pub/source/v11.4/postgresql-11.4.tar.gzCopy the code

Note: If the download is slow, you can directly take the installation package attached to this article.

Decompress and view the directory using the following command:

$tar ZXVF postgresql 11.4. Tar. GzCopy the code

To compile the source code, run the following command:

    $ ./configure --prefix=/usr/local/postgresql
Copy the code

To install, run the following command:

    $ make && make install
Copy the code

Go to the installation directory and view the directory structure as follows:

    $ cd /usr/local/postgresql/
Copy the code

To create directories data and log, run the following command:

$ mkdir /usr/local/postgresql/data
$ mkdir /usr/local/postgresql/log
Copy the code

To add system environment variables, run the following command:

    $ vim /etc/profile
Copy the code

Write the following at the end:

export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
Copy the code

To make the configuration file take effect, run the following command:

    $ source /etc/profile
Copy the code

To add user postgres, assign permissions and reset passwords, run the following command:

$ adduser postgres
$ passwd postgres
$ chown -R postgres:root /usr/local/postgresql/
Copy the code

Switch to user postgres and run the following command to initialize the database:

$ su postgres
$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
Copy the code

Note: You cannot initialize the database as user root, otherwise an error will be reported

Edit the postgresql.conf file and change it to the following content:

$ vim /usr/local/postgresql/data/postgresql.conf
listen_addresses = '*'
port = 5432
Copy the code

Edit the pg_hba.conf file and add remote access to the last line as follows:

    $ vim /usr/local/postgresql/data/pg_hba.conf
Copy the code

  

To change the PostgreSQL initial password, log in to the PostgreSQL database, run the following command to change the password, and exit the database:

$ psql -U postgres -d postgres
$ ALTER USER postgres WITH PASSWORD 'postgres';
$ \q
Copy the code

Run the following command to create a soft link for pg_ctl:

    $ ln -s /usr/local/postgresql/bin/pg_ctl /usr/bin/pg_ctl
Copy the code

To start the service, run the following command:

    $ pg_ctl start -l /usr/local/postgresql/log/pg_server.log
Copy the code

Three, validation,

To view the version, run the following command:

    $ psql -V
Copy the code

A third-party tool tests the connection, as shown below:

  

The PostgreSQL installation is complete.