一、下载源码

  1. [root@VM-0-13-centos src]# cd /usr/local/src
  2. [root@VM-0-13-centos src]# wget https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.gz

二、检查编译环境

postgrepsql在编译时需要make 3.80以上版本,若版本低于此版本,需要升级较新版本

[root@VM-0-13-centos src]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[root@VM-0-13-centos src]# make --version 
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

三、检查扩展包

# 服务器端编程语言PL/Perl,则需要一个完整的 Perl安装,包括libperl 库和头文件。Perl 的最低版本是 5.8.3
[root@VM-0-13-centos src]# perl --version

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 40 registered patches, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


# 编译PL/Python服务器端编程语言,需要一个Python的安装,包括头文件和distutils模块。Python的最低版本是2.4
# 现在官方还没有将默认的python支持迁移到python3(https://www.postgresql.org/docs/13/plpython-python23.html),建议python用户使用python2.6或2.7

[root@VM-0-13-centos postgresql-13.3]# python --version
Python 2.7.5

# 若需要使用openssl生成随机数,openssl的版本最低为0.9.8
[root@VM-0-13-centos postgresql-13.3]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

# 其他扩展软件检查,可以参考官方文档
# https://www.postgresql.org/docs/13/install-requirements.html

四、编译安装PostgreSQL

pgsql常用预配置如下,更多配置内容可参考官方文档
—prefix=/usr/local/pgsql pgsql 默认安装目录
—bindir=/usr/local/pgsql/bin pgsql 可执行文件目录
—sysconfdir=/usr/local/pgsql/etc pgsql 配置文件目录
—libdir=/usr/local/pgsql/lib pgsql 安装库和动态装载模块的目录
—includedir=/usr/local/pgsql/include pgsql c和c++头文件
—datarootdir=/usr/local/pgsql/share pgsql 设置只读数据文件的根目录
—datadir=/usr/local/pgsql/data pgsql 只读数据文件的目录
—localedir=/usr/local/pgsql/locale pgsql 设置安装区域数据的目录
—mandir=/usr/local/pgsql/man pgsql man手册目录
—docdir=/usr/local/pgsql/doc/postgresql pgsql 设置安装文档文件的根目录
—htmldir=/usr/local/pgsql/share pgsql HTML格式的文档将被安装在这个目录
—with-pgport=5432 pgsql 设置端口(默认5432)
—with-openssl pgsql 配置openssl

[root@VM-0-13-centos postgresql-13.3]# ./configure --prefix=/usr/local/pgsql \
--bindir=/usr/local/pgsql/bin \
--sysconfdir=/usr/local/pgsql/etc \
--libdir=/usr/local/pgsql/lib \
--includedir=/usr/local/pgsql/include \
--datarootdir=/usr/local/pgsql/share \
--datadir=/usr/local/pgsql/data \
--localedir=/usr/local/pgsql/locale \
--mandir=/usr/local/pgsql/man \
--docdir=/usr/local/pgsql/doc/postgresql \
--htmldir=/usr/local/pgsql/share \
--with-pgport=5432 \
--with-openssl

在这里编译失败,报错如下图
image.png
readline是psql中的命令行编辑和历史,官方不建议禁用,解决方案如下

[root@VM-0-13-centos postgresql-13.3]# yum -y install -y readline-devel

image.png

yum -y install python python-devel

image.png

yum -y install openssl-devel

image.png

yum -y  install perl-ExtUtils-Embed

安装readline-devel后pgsql预配置成功,下面开始编译,此过程将持续较长时间

[root@VM-0-13-centos postgresql-13.3]# make
[root@VM-0-13-centos postgresql-13.3]# make all
[root@VM-0-13-centos postgresql-13.3]# make install

五、安装后配置

配置环境变量

[root@VM-0-13-centos pgsql]# cat >>/etc/profile<<EOF
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PATH=/usr/local/pgsql/bin:\$PATH
EOF
[root@VM-0-13-centos pgsql]# source /etc/profile

创建postgresql用户

[root@VM-0-13-centos pgsql]# useradd postgres
[root@VM-0-13-centos pgsql]# passwd postgres

修正权限

[root@VM-0-13-centos pgsql]# chown -R postgres.postgres /usr/local/pgsql

初始化目录集簇

[root@VM-0-13-centos pgsql]# su - postgres
[postgres@VM-0-13-centos pgsql]$  /usr/local/pgsql/bin/pg_ctl -D /home/postgres/data initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /home/postgres/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/pgsql/bin/pg_ctl -D /home/postgres/data -l logfile start

启动服务

[postgres@VM-0-13-centos pgsql]$ /usr/local/pgsql/bin/pg_ctl -D /home/postgres/data -l logfile start

配置远程连接postgresql


#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'                             # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories

配置连接方式

# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.

# CAUTION: Configuring the system for local "trust" authentication
# allows any local user to connect as any PostgreSQL user, including
# the database superuser.  If you do not trust all your local users,
# use another authentication method.


# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             myuser             0.0.0.1/0            md5  # 限制myuser用户可以通过username/password 的方式访问
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

创建用户并授权

[postgres@VM-0-13-centos pgsql]$ psql
psql (12.10)
Type "help" for help.
postgres=# create database mydb
postgres=# create user myuser with password 'mypassword';
CREATE ROLE
postgres=# CREATE DATABASE mydb;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
GRANT
postgres=# GRANT ALL PRIVILEGES ON all tables in schema public TO myuser;
GRANT

远程连接数据库

postgresql01.pngpostgresql02.png