OpenEuler20.03 source code installation and configuration PostgreSQL13.4 detailed graphic version

OpenEuler installation and configuration PostgreSQL

serial number Update details Updated updater
1 Completed the content editing of the first to third chapters; September 18, 2021 lol
2 Add PostgreSQL service to start automatically at boot; October 25, 2021 lol

1. Preparatory conditions

OpenEuler (Hyper-V virtual machine):

PostgreSQL:

  • Version: 13.4 source version

  • Download address: https://www.postgresql.org/download/

  • Because PostgreSQL has not released a packaged version for the OpenEuler distribution, we need to download the source code to compile and install it ourselves. If there is a packaged version for other Linux distributions, you can download and install it yourself.

    Source code download location:



*Select the latest version of the tar.gz compressed file of the current stable version, you can copy and right-click to copy the download link, and save it for later use.

Download link: https://ftp.postgresql.org/pub/source/v13.4/postgresql-13.4.tar.gz

2. Install OpenEuler

2.1 Installation Tips

The installation of the operating system can be carried out according to the prompts. When selecting the software package, the Server mode is selected this time, and the software package on the right remains unselected by default.

Follow the prompts and restart to enter the login interface (command line mode) after the installation is complete. This installation does not install a graphical interface.

Note: After the system starts, the default DHCP automatically assigns IP, and the network needs to be configured first.

2.2 Network configuration

After logging in successfully, enter the following command to get the network configuration file of the current host:

/** 1.切换到网络配置文件目录 **/
cd /etc/sysconfig/network-scripts/

/** 2.查看配置文件名称 **/
ls 

The obtained configuration file name is as follows:

The name of the configuration file is "ifcfg-eth0". At this time, you can use "vi file name" to edit the file. If you are familiar with the vi tool, the above operations can be processed by one command:

vi /etc/sysconfig/network-scripts/ifcfg-eth0

The default configuration is as follows:

The configuration items that need to be modified are as follows:

  • BOOTPROTO: Change dhcp to none, which means that the IP address will be assigned statically;
  • ONBOOT: Change no to yes, which means that the device is automatically enabled at startup;
  • Newly added: IPADDR=192.168.12.132 //represents the assigned IP address, which can be set here according to the situation;
  • New: GATEWAY=192.168.12.254 //default gateway
  • Added: PREFIX=24 //mask
  • Newly added: DNS1=202.102.224.68 //Default DNS, multiple DNS will increase by serial number;

The modified configuration file is as follows:

Save and exit vi editing mode after modification.

  • After vi opens the file, you need to press the "i" key to enter the editing mode. After editing, press the "Esc" key first, and then enter ":wq" to save and exit.

Enter the following command to restart the network service:

  systemctl restart NetworkManager

If there is no error prompt, it means that the restart is successful. You can use the ifconfig command or "ip a" command to check the current network status, and use the ping command to check whether the network is smooth.

**If you are still prompted that the service is unavailable after restarting the network at this time, and you cannot ping each website, you can disable the current network card and restart it. (After testing, it is found that the OpenEuler virtual machine created with Hyper-V occasionally has this situation, and the VM is directly connected to the Internet after configuration.) **Commands are as follows:

# 查看 网卡信息
nmcli con show

# 停用 指定网卡 (eth0为查询到的配置网卡信息)
nmcli con down eth0

# 启用 指定网卡
nmcli con up eth0

At this time, the network is smooth, and ssh is enabled by default. At this time, the system can be managed through ssh connection, and the subsequent instructions are all processed through ssh. After the ssh login is successful, as shown in the figure:

3. Install PostgreSQL

3.1 Installation steps

  • Read the official source code installation instructions;

  • Download the source package;

  • According to the official instructions, installation must depend on;

  • Compile and install PostgreSQL according to the official instructions;

  • Configure and start PostgreSQL;

If there is no special instruction, this instruction is to log in and install with the root account, so the sudo prefix is ​​no longer required for special system permission commands. If the root account is not used in the actual installation process and the installation error occurs, you can try to add sudo before the command.

3.2 Official Installation Instructions

Official source code installation documentation address: https://www.postgresql.org/docs/current/installation.html

How to find the entrance:

After opening the document, the 16.1Short Version chapter gives the basic installation commands, other related configuration instructions can be found in other chapters, and the 16.2Requirements chapter explains the related dependencies, it is recommended to read.

3.3 Download source package

1. First, ssh connects to the server, and the connection command:

ssh [email protected]

After pressing Enter, enter the root password and press Enter: Seeing the following screenshot means that the login is successful:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-f6I9xZYr-1678067940221)(images/image-20210918123846921.png)]

2. After logging in with ssh, it is in the root directory of the root user by default. We can create a tools file and place the downloaded source package in this folder. The command is as follows:

mkdir tools
cd tools
wget https://ftp.postgresql.org/pub/source/v13.4/postgresql-13.4.tar.gz

The download is complete, see the figure below:

3.4 Installation dependencies

According to the documentation, the necessary dependencies are as follows:

  • make requires version 3.80+

  • gcc

  • tar

  • Readline

  • zlib

Other optional dependencies can be selected according to their own needs.

3.4.1make

To check the make version of the current system, you can use the following command:

make --version

The obtained version number is 4.3, which meets the installation requirements and does not need to be installed again;

3.4.2gcc
gcc -v

It is found that gcc has been installed, and the version is 7.3.0, which meets the installation conditions;

3.4.3tar

tar is the decompression software, the system has built-in, no need to install;

3.4.4readline

Readline library: It allows psql to remember each command you enter, so that you can quickly enter the previous command through the up and down arrow keys. It is enabled by default, and it can also be disabled by compiling the parameter --without-readline. It is recommended to keep the default.

The readline library system has been built in, but OpenEuler needs to install the readline-devel library when installing PostgreSQL.

Install command:

dnf install readline-devel

or

yum install readline-devel
3.4.5 zlib

zlib is the default compression library, same as readline, the zlib system is built-in by default, but the zlib-devel library needs to be installed additionally.

Install command:

dnf install zlib-devel

or

yum install zlib-devel
3.4.6 Others

The missing libraries can also be installed uniformly with one command:

dnf install readline-devel zlib-devel

or

yum install readline-devel zlib-devel

3.5 Compile and install

step:

  • Unzip the source package
  • Compile and install according to the official simple version installation command
  • Supplementary operation
3.5.1 Unzip the source package

Go back to the tools folder, use the tar tool to decompress the source code package to the current directory, the command is as follows:

tar -xvf postgresql-13.4.tar.gz

After decompression, a postgresql-13.4 folder is generated under the current tools.

3.5.2 Explain the official installation command

The installation command of the simple version provided by the official is as follows:

./configure                       ##配置
make                              ##编译
su                                ##切换管理员权限
make install                      ##安装
adduser postgres                  ##添加postgres用户
mkdir /usr/local/pgsql/data       ##创建data存放目录
chown postgres /usr/local/pgsql/data   ##给postgres用户授权
su - postgres                     ##切换到postgres用户(数据库的操作不允许使用root用户)
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data  ##初始化数据库
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start   ##启动数据库
/usr/local/pgsql/bin/createdb test  ##创建test数据库
/usr/local/pgsql/bin/psql test      ##连接test数据库

The official command is relatively simple, here needs to explain, some operations will modify the official command:

The configure file is an executable script file with many options. Use the command ./configure –help in the source code directory to be installed to output a detailed list of options.

Among them, the --prefix option is to configure the installation directory. If this option is not configured, the executable file is placed in /usr/local/bin by default after installation, the library file is placed in /usr/local/lib by default, and the configuration file is placed in /usr/ by default. local/etc, and other resource files are placed in /usr/local/share, which is messy.

If --prefix is ​​configured, such as:

./configure --prefix=/usr/local/test

All resource files after installation will be placed in the /usr/local/test directory and will not be scattered to other directories.

*Here we configure the path as:

 ./configure --prefix=/usr/local/postgresql	

Other modified commands will be explained separately in the compilation and installation chapter.

3.5.3 Compile and install

Now we proceed item by item.

Please ensure that the current directory is: /tools/postgresql-13.4/

3.5.3.1 configure

Excuting an order

./configure --prefix=/usr/local/postgresql

No error should be reported after the correct execution:

注:If an error message indicates that there is no specified package, you can install or exclude the specified package offline. For details, see: https://blog.csdn.net/asd54090/article/details/129355835

3.5.3.2 make

Enter the make command and press Enter. The compilation process takes a long time. Please wait patiently for the compilation to end.

Order:

make

If there is no error word in the compilation process, it means that the compilation ends normally.

3.5.3.3 are

This command is to switch the root management authority, but we use the root account for the current operation, so this command does not need to be executed. If you use a non-root account, please execute the su command first.

3.5.3.4 make install

Order:

make install

If there is no error word in the compilation process, it means that the compilation ends normally.

3.5.3.5 adduser postgres

Order:

adduser postgres

Create a postgres user and user group, the default password is random, and there is no error prompt after pressing Enter, which means the creation is successful;

3.5.3.6 mkdir /usr/local/pgsql/data

Above we changed the installation address to our custom address, this command needs to be modified:

mkdir /usr/local/postgresql/data

3.5.3.7 chown postgres /usr/local/pgsql/data

Also the command is changed to:

chown postgres /usr/local/postgresql/data

3.5.3.8 su - postgres

Switch to the postgres user

3.5.3.8 Database Operation

Because we have modified the installation location, the following commands must be modified according to the actual installation location.

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data  ##初始化数据库

change into:

/usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data  ##初始化数据库

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start   ##启动数据库

change into:

/usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data -l logfile start   ##启动数据库

Started successfully.

/usr/local/pgsql/bin/createdb test  ##创建test数据库

change into:

/usr/local/postgresql/bin/createdb test  ##创建test数据库

/usr/local/pgsql/bin/psql test      ##连接test数据库

change into:

/usr/local/postgresql/bin/psql test      ##连接test数据库

The appearance of "test=#" means that the test database has been successfully connected, and related SQL operations can be performed.

3.5.4. Supplementary operations
3.5.4.1 Configure database remote connection

After the above operations, the database has been successfully installed, and you can check the background process through the ps command to confirm:

However, it is impossible to connect externally through database tools, so we need to configure postgres simply.

Enter the data directory under postgrelsql, the command and directory structure are as follows:

The base directory is the table space directory, the global directory is the relevant global variable directory, pg_hba.conf is the access control configuration file, and postgresql.conf is the main postgresql configuration file. The configuration files we need to modify are pg_hba.conf and postgresql.conf files.

Modify the pg_hba.conf file, the command is as follows:

vi pg_hba.conf	

Scroll to the bottom and find the following picture:

Modify the contents of the red box to read:

Save and exit.

Modify the postgresql.conf file, the command is as follows:

vi postgresql.conf

Find the "# - Connection Settings -" configuration item in the expanded content:

Modify the content in the red circle, remove the front "#", change 'localhost' to '*', keep other configuration items unchanged, save and exit.

After the configuration is complete, port 5432 should be allowed on the firewall. At this time, using the postgres user (the password is random, if you modify the postgres password by yourself, you can execute it directly) cannot be executed. You should first log out of the postgres account and return to the root account. Execute the command as follows:

exit    ##退出postgres账号

##以下命令在root账户下执行
sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent     ##配置防火墙放行5432端口
sudo firewall-cmd --reload       ##防火墙重新加载配置文件

At this time, switch back to the postgres account, stop the postgressql service, restart and find that it cannot be started, and the error is as follows:

Reason for error: The path of the startup log is not specified. The logfile in the start command and stop command needs to be specified as a specific log log command. At this time, we use the postgres account to create the log folder in the postgresql directory without permission. In chapter 3.5.3.7, we only authorized the data directory for the postgres account. We do the following:

  • 1. Switch back to the root account;
  • 2. Authorize the permissions of the /usr/local/postgresql directory to the postgres account;
  • 3. Then switch back to the postgres account to create the log folder;

Create a server.log file under the log folder:

The command is as follows:

touch log/server.log	

After the creation is successful, modify the startup command to:

/usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/log/server.log start

Execute the command and start successfully.

At this time, using an external sql tool such as navicat to test the network is smooth, but we need to modify the default password of the database account postgres to connect. First, use the local postgres account to connect to the default database to modify the password of the database account postgres:

/usr/local/postgresql/bin/psql      ##使用postgres用户连接默认postgres数据库;
ALTER USER postgres WITH PASSWORD 'postgres';     #修改数据库用户postgres的密码为postgres;

At this point, use Navicat to test the database connection is successful.

3.5.5.2 Configure environment variables

As of now, if we want to use psql or other provided tools to connect to the database on the server where postgresql is installed, we must enter the full path of psql to call, such as:

/usr/local/postgresql/bin/psql 

It is not very convenient for us to use. At this time, we can configure postgresql into the system environment variable. Note: Please configure it in the bash_profile environment variable of the postgres user or other users who customize postgresql, and do not configure the environment variable of the root account. In, or can be configured in etc/profile, it cannot be used when it is configured separately in the root environment variable.

Please configure as root user:

vi /etc/profile

##将如下内容添加到文件末尾
PATH=/usr/local/postgresql/bin:$PATH
export PATH

##保存退出后执行刷新配置
source /etc/profile

##切换到postgres用户
su - postgres

##此时就可以直接执行psql命令了
psql

3.5.5.3 Set PostgreSQL to start at boot

PostgreSQL's automatic startup script is located in the contrib/start-scripts directory of the PostgreSQL source code directory: the

linux file is the startup script in the Linux environment, copy it to the "/etc/init.d" directory, and rename it for postgresql:

cp linux /etc/init.d/postgresql

Switch to the /etc/init.d directory, use vi to open the postgresql file and modify the following content:

#修改前:
#Installation prefix
prefix=/usr/local/pgsql

#修改后: 
#Intalllation prefix[2021-10-25]
prefix=/usr/local/postgresql

#修改前:
# Data directory
#PGDATA="/usr/local/pgsql/data"

#修改后:
# Data directory[2021-10-25]
PGDATA="/usr/local/postgresql/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")
# 将启动用户修改为实际用户,这里使用postgres,所以不变。
PGUSER=postgres

#修改前:
# Where to keep a log file
PGLOG="$PGDATA/serverlog"

#修改后:
# Where to keep a log flie[2021-10-25]
PGLOG="/usr/local/postgresql/log/server.log"

Then save and exit.

Then modify the execution permissions of the postgresql file:

chmod a+x postgresql

Add boot start:

chkconfig --add postgresql

Then restart the server and start the verification.

Reprinted to: https://www.cnblogs.com/boyliupan/p/15324331.html

Guess you like

Origin blog.csdn.net/asd54090/article/details/129355636