Objective
The previous blog records the installation of PostgreSQL 9.3 in the form of RPM packages (Linux CentOS 7 Installation PostgreSQL 9.3 (release)), and this blog will record the installation of PostgreSQL 9.5 in the form of source code compilation.
Download
In the official PostgreSQL can find the source file directory, the address is as follows: https://www.postgresql.org/ftp/source/, in the download list according to the needs of the selected version, such as:
After entering the subdirectory, you can see the list of files:
As you can see, there are two compression formats available, where we select postgresql-9.5.5.tar.gzand upload to the specified directory on the CentOS server when the download is complete.
Configuring the compilation Installation
First into the PG compression package directory by tar -zxvf ./postgresql-9.5.5.tar.gz
decompression, and then you can start to compile the installation, into the decompression directory, through ./configure --help
the can see the compilation related help information, such as:
For example, you --prefix=dir
can specify the installation directory, and you --with-python
can use the Python syntax of the Pl/python process language custom function, according to requirements we do not use, so at compile time only specify an installation directory:
./configure --prefix=/usr/local/postgresql
Running compile-time discovery prompts you not to have a C compiler because of the new system, so installing a GCC compiler is possible:
install gcc
After the installation is complete, compile the postgres again and find that the ReadLine library is missing, such as:
For example, but through the rpm -qa | grep readline
command view you can find that the system is self-readline package by default, such as:
Then there must still be a lack of readline-related packages, through the yum search readline
search can find a readline-devel package, such as:
In fact, what we lack is this readline development package (Readline-devel), not the ReadLine package, so install readline-develNext:
readline-devel
After installation is completed again PostgreSQL, or error, this time the hint is missing zlib library:
Similarly, the missing is still the zlib development package (Zlib-devel) instead of the zlib package, so continue with the installation Zlib-devel:
install zlib-devel
After the installation is complete and the PostgreSQL is compiled again, and no more error is reported, you can see the prompt to create the config.status configuration file:
The configuration is complete, and then it is ready to be compiled and installed, followed by:
makemake install
See the following tips to illustrate the success of the build installation:
User rights and environment variables
After the successful compilation and installation, the next thing to do is to create a normal user, because the default superuser (root) does not 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 all Postgres data directories to the Postgres user (here I'll specify the Postgres data directory in the/usr/local/postgresql/data directory):
chown -R postgres:postgres /usr/local/postgresql/
Finally, for the sake of convenience set up the related environment variables, here just set Postgres user's environment variables, so first by su - postgres
switching to Postgres user, open the . Bash_profile file and append the following:
After the modification is complete, you can source ./.bash_profile
make it effective immediately, then verify that the environment variable is set correctly, switch any directory input, which psql
and psql -V
view the path to the Psql client and the database version of PostgreSQL, as follows:
After everything is ready, the database can be initialized in the next step.
Initializing the database
Because the environment variable is configured, we can do it directly here initdb
, but before we do that we could initdb --help
look at the initialization-related help information:
As you can see, when using initdb
initialization we can specify parameters to do some initialization work at the same time, such as specifying Pgdata (PostgreSQL data directory), specifying encoding (encoding), specifying the user name and password of the database Superuser, and so on. In the last part of the paragraph I marked out that if the data directory is not specified, the Pgdata in the environment variable will be used by default, since we have just set up the PGDATA environment variable, so here we do not need to specify the additional, the final execution of the initialization command:
initdb
The following information indicates that the initialization was successful:
At the same time, in the PostgreSQL directory, you can see the data directory and the data and configuration files for that directory:
For example, the base directory is the table space directory, and the global directory is the directory of the related globals, Pg_ Hba.conf and Postgresql.conf also mentioned in previous blogs that one is the access control configuration (127.0.0.1 to a trusted client IP network segment so that it can be accessed remotely), and one is the PostgreSQL Master profile (listen_address =localhost change to an asterisk to make it listen to the whole network), for convenience I have changed the IP address of the pg_hba.conf to, and the 0.0.0.0/0
encryption mode instead, means that md5
requires password access, is to provide a lowest level of security protection:
And postgresql.conf, like the above, modifies the listen_address so that it can listen to the entire network:
Finally, do not forget to open the PG 5432 port, or even if the above two modifications the client still cannot connect PostgreSQL, so add 5432 port to zone, run the following command (note that this needs to be cut back to the root user, otherwise there is no permission):
firewall-cmd--zone=public--add-port=5432/tcp--permanentfirewall-cmd--reload
The configuration is now complete, you can also firewall-cmd --zone=public --list-ports
check the list of open ports again to confirm that the following prompt indicates that the port has been successfully opened:
This configuration-related content is all done, and finally the database is started and connected.
Start and connect
We have seen the start command at the end of the initialization database, such as:
Since we have set the environment variable, we have specified the data directory Pgdata, which means that the -l
log file directory is usually specified, so we/usr/local/ In the PostgreSQL root directory, create a log directory to hold the journal files (note that you don't forget to give writable permissions),
The last run pg_ctl start -l /usr/local/postgresql/log/pg_server.log
can start the database, see the following prompt to indicate that the boot succeeded:
or ps -ef|grep postgres
, check to see if there are processes related to Postgres, as well as the success of the startup:
After the successful launch, we can connect via PostgreSQL's client tool Psql , and the direct input to psql
see the version information indicates the connection was successful:
In the log file directory, you can see the database log file and the boot log that you just started:
The next thing to do is to set the Postgres user's password (default is empty), with the Psql connection is successful, the direct input \password
will prompt for two password, such as:
Here we will temporarily change the password to 111111, through the \l
command to view the list of databases, about the use of psql tools here will not do too much to repeat. The final and most important point is to verify the connection of non-local client tools, after all, we are to do database server, here I choose the tool is Navicat Premium, On the host (WINDOWS7), open the Navicat connection test with the PostgreSQL server in the virtual machine:
For example, you can find that the connection is successful, the same as stopping the database can use the command pg_ctl stop
to shut down the PostgreSQL service, very simple, this is about the source code compiled and installed PostgreSQL is all over.
Summarize
Simply record the entire process of compiling and installing the postgreSQL9.5 version in Linux CentOS 7, hoping to help friends with the same problem, the End.
Linux CentOS 7 Installation PostgreSQL 9.5 (source code compilation)