Previous blogs recorded the installation of PostgreSQL 9.3 via the RPM package (Linux CentOS 7 Installation PostgreSQL 9.3 (release)), this blog will be recorded in the form of the source code compiled PostgreSQL 9.5.
Download
The source file directory can be found in the official PostgreSQL, the address is as follows: https://www.postgresql.org/ftp/source/, select the version according to the request in the download list, the following figure:
After entering subdirectories, you can see a list of files:
As shown above, you can see that there are two compression formats, where we choose postgresql-9.5.5.tar.gzand upload to the CentOS server's designated directory when the download is complete.
Configure compilation Installation
First into the PG compression package directory through the TAR-ZXVF./postgresql-9.5.5.tar.gz for decompression, and then you can start compiling the installation, into the decompression directory, through./configure--help can see compilation-related Help information, as shown in the following figure:
As shown above,--prefix=dir can specify the installation directory, and, for example, --with-python can use the Python syntax of the Pl/python process language customization function, according to the requirements of our temporary use, So only one installation directory can be specified at compile time:
./configure--prefix=/usr/local/postgresql
After you run the compile, you will be prompted that there is no C compiler because of the new system, so install a GCC compiler:
Yum Install GCC
After the installation is finished, compile the Postgres again, and discover that the ReadLine library is missing, as shown in the following figure:
As shown above, but with the Rpm-qa | grep readline Command view you can find that the system defaults to the ReadLine package, as shown in the following figure:
Then there is definitely a lack of readline-related packages, searching through Yum search ReadLine can find a readline-devel package, as shown in the following figure:
What we're missing is the ReadLine Development Kit (Readline-devel), not the ReadLine package, so then install readline-devel:
Yum Install Readline-devel
After the installation completes compiles PostgreSQL again, or the error, this time prompts the missing zlib library:
Similarly, the missing is still the zlib development package (Zlib-devel) rather than the zlib package, so continue installing Zlib-devel:
Yum Install Zlib-devel
After the installation is completed, compile PostgreSQL again, and no more error, you can see the prompts to create the Config.status profile:
The configuration is complete, and then you can compile the installation and execute it sequentially:
Make
Make install
You can see the following prompts to illustrate the successful compilation installation:
User rights and environment variables
After compiling the installation successfully, the next thing to do is to create a normal user, because the default superuser (root) cannot start PostgreSQL, so you need to create a normal user to start the database, execute the following command to create the user:
Useradd Postgres
Next you need to set permissions to assign the Postgres data directory to the Postgres user (here I specify the Postgres data directory in the /usr/local/postgresql/data directory):
Chown-r postgres:postgres/usr/local/postgresql/
Finally, for convenience, set the relevant environment variables, here only set the Postgres user's environment variables, so first through the su-postgres switch to the Postgres user, open the . Bash_profile file and append the following:
After the modification is complete, you can make it effective immediately by source./.bash_profile, and then check that the environment variable is set correctly, switch any directory input which psql and Psql- V You can view the path of the Psql client separately and the database version of PostgreSQL, as shown in the following figure:
After everything is ready, you can then initialize the database.
Initializing the database
Because the environment variable is configured, we can perform DB initialization directly from here, but before that we could take a look at initialization-related help through Initdb--help: Initdb.
As shown above, you can see that when initializing with INITDB, we can specify parameters to perform some initialization work at the same time, such as specifying the Pgdata (PostgreSQL data directory), specifying encoding (encoding), specifying the username and password of the database Superuser, and so on, In the end, I flagged the passage to indicate that if the data directory is not specified, the Pgdata in the environment variable is used by default, and since we have just set the PGDATA environment variable, we do not need to specify this here, and finally execute the initialization command:
Initdb
The following message indicates that the initialization was successful:
At the same time in the PostgreSQL directory you can see the generated data directory and the relevant data and configuration files for that directory:
As shown above, the base directory is a table space directory, and the global directory is a directory of related global variables, pg_ Hba.conf and Postgresql.conf also mentioned in previous blogs that one is an access control configuration (127.0.0.1 to a trusted client IP segment so that it can be accessed remotely), and one is the PostgreSQL main configuration file (listen_address =localhost to the asterisk to monitor the entire network), for convenience, I will change the IP address of pg_hba.conf to 0.0.0.0/0, and the encryption mode to MD5, which means that password access is required to provide a lowest level of security protection:
And postgresql.conf, like the above, modifies the listen_address to listen to the entire network:
Finally, don't forget to open PG 5432 port, or even if you do the above two modifications to the client still cannot connect PostgreSQL, so add 5432 port to zone, run the following command (note that you need to cut back to the root user, otherwise do not have permissions):
Firewall-cmd--zone=public--add-port=5432/tcp--permanent
Firewall-cmd--reload
The configuration is now complete, but also through the firewall-cmd--zone=public--list-ports to view the list of open ports to confirm again, see the following prompts to indicate that the port has been successfully opened:
This configuration-related content is complete, and finally the database is started and connected.
Start and connect
We have seen the launch command at the end of the initialization database, as shown in the following figure:
Since we have set the environment variable, we have already specified the data directory pgdata, -1 represents the log file directory, usually needs to be specified, so we /usr/local/postgresql A log directory is created in the root directory to store the logs (note that you do not forget to grant writable permissions).
The last run pg_ctl start-l/usr/local/postgresql/log/pg_server.log can start the database, see the following prompts to explain the success of the launch:
or through the Ps-ef|grep postgres to see if there is a related process Postgres related, the following figure can also indicate successful start:
After the success of the launch, we can use the PostgreSQL from the client tool Psql to connect, direct input psql see version information to indicate the success of the connection:
In the log file directory, you can see the database log file and the startup log that you just started:
The next thing to do is to set the Postgres user's password (the default is empty), the Psql connection after the success of the direct input \password will be prompted to enter the password two times, the following figure:
Here we will modify the password to 111111, through the \l command to view the list of the database, the use of psql tools in this place will not do too much to repeat. The last and most important point is to verify the connection of the Non-local client tool, after all, we are going to do the database server, here I choose the tool is Navicat Premium, On the host (WINDOWS7), open the Navicat and test the connection to the PostgreSQL server in the virtual machine:
As shown above, you can find the connection is successful, the same time stop the database can use pg_ctl stop
the command to shut down the PostgreSQL service, very simple, so far on the source code compiled installation PostgreSQL has been all over.
Summarize
Simply record the postgreSQL9.5 version in Linux CentOS 7 in the entire process of compiling and installing, hoping to help friends with the same problem, the end.