1.下载指定版本的源码
下载地址:http://www.postgresql.org/ftp/source/ ,下载17 版本为例
wget https://ftp.postgresql.org/pub/source/v17.0/postgresql-17.0.tar.gz
2.安装依赖
dnf install -y gcc gzip bzip2 tar perl perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake libicu-devel gcc gzip bzip2 tar systemd-devel
在 17 版本中,新增两个语法解析器?在编译安装 16 版本时,未安装以下两个解析器。
Bison - 一个强大的语法分析器生成器
Bison是Linux下的一个语法分析器生成器,用于将上下文无关文法转换为C代码,简化编译器或解释器开发。它提供性能优化和灵活的语义动作定制,常用于创建解析器,如SQL解析器或自定义脚本语言解释器。通过编写.y文件定义语法规则,使用Bison生成解析器代码,然后集成到项目中,搭配词法分析器如Flex使用。Bison帮助开发者专注于应用逻辑,而非解析器实现。
在软件开发中,特别是当涉及到编译器或解释器的编写时,语法分析是一个至关重要的步骤。Bison,作为一个强大的语法分析器生成器,极大地简化了这一复杂过程。本博客将带您深入了解 Bison,包括其基本概念、使用方法以及它在项目中的应用。
什么是 Bison?
Bison(之前称为 Yacc - Yet Another Compiler-Compiler)是一个将上下文无关文法(Context-Free Grammars)转化为 C 语言代码的工具。这些生成的代码可以进一步被用作解析器(Parser),将输入的文本字符串转化为抽象语法树(Abstract Syntax Tree, AST)或其他形式的中间表示。
为什么要使用 Bison?
- 简化开发:通过 Bison,开发人员可以专注于语法规则的定义,而不需要手动编写复杂的解析器代码。
- 性能:生成的 C 代码可以经过优化,以提供高效的解析性能。
- 灵活性:Bison 支持多种类型的语法和语义动作,允许开发人员根据需求定制解析器的行为。
在大多数 Linux 发行版中,您可以通过包管理器来安装 Bison。
dnf install bison
Flex
在Linux上,Flex是一个非常强大的词法分析器生成器。它通常用于编写编译器和解释器中的词法分析器。Flex可以根据用户定义的正则表达式规则,将输入文件分割为一系列的标记(token),然后将这些标记传递给后续的语法分析器进行进一步处理。
要在Linux上使用Flex命令,首先需要安装Flex工具。可以通过以下命令安装Flex:
dnf install flex
安装完成后,可以使用flex命令来生成词法分析器。
3.源码编译安装
3.1.编译postgresql源码
tar -zxvf postgresql-17.0.tar.gz
cd postgresql-17.0
./configure --prefix=/opt/postgresql --with-systemd
在生产环境,一般都会用 systemd 来管理服务,而非直接使用 pg_ctl 来进行数据库启停操作。
源码编译时,需要传递参数 --with-systemd 源码会对此进行一些优化处理,比如服务启动超时时间。 但使用此参数需要先安装依赖 systemd-devel,否则会报错:configure: error: header file <systemd/sd-daemon.h> is required for systemd support
编译安装,依据cpu核心数,可以指定多个并行度加快编译速度:
make -j 4 && make install
如是想将contrib下的扩展一同安装,使用
make world
make install world
清理编译时产生的临时文件
make clean
3.2.创建用户、用户组
groupadd postgres
useradd -g postgres postgres
echo "Admin123" | passwd --stdin postgres
3.3.创建数据主目录
mkdir -p /opt/postgresql/{pgdata,archive,scripts,backup,soft}
chown -R postgres:postgres /opt/postgresql
chmod -R 775 /opt/postgresql
3.4.配置环境变量
cat >> /etc/profile <<EOF
# Postgresql Environment Setting
export PGHOME=/opt/postgresql
export PGDATA=/opt/postgresql/pgdata
export LD_LIBRARY_PATH=/opt/postgresql/lib
export PATH=$PATH:$HOME/bin:/opt/postgresql/bin
EOF
立即生效
source /etc/profile
3.5初始化数据库
[root@postgresql-server ~]# su - postgres
[postgres@postgresql-server ~]$ /opt/postgresql/bin/initdb -D /opt/postgresql/pgdata/
......
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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:
/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgdata/ -l logfile start
[postgres@pgcp-01 ~]$
initdb:警告:为本地连接启用“信任”身份验证
initdb:hint:您可以通过编辑 pg_hba.conf 或在下次运行 initdb 时使用选项 -A或–auth-local和–auth-host来更改这一点。
成功现在可以使用以下命令启动数据库服务器:/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgdata/ -l logfile start
启动实例
[postgres@pgcp-01 ~]$ /opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgdata/ -l logfile start
waiting for server to start.... done
server started
[postgres@pgcp-01 ~]$
创建数据库
[postgres@pgcp-01 ~]$ /opt/postgresql/bin/createdb byte
[postgres@pgcp-01 ~]$ /opt/postgresql/bin/psql byte
psql (17.0)
Type "help" for help.
byte=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
byte | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
byte=#
3.6.配置服务
PostgreSQL的开机自启动脚本位于postgreSQL源码目录的contrib/start-scripts路径下。
[root@postgresql-server start-scripts]# pwd
/root/postgresql-17/contrib/start-scripts
[root@postgresql-server start-scripts]# ls -la
total 12
drwxrwxrwx. 3 1107 1107 47 Nov 7 06:17 .
drwxrwxrwx. 61 1107 1107 4096 Nov 7 06:17 ..
-rw-r--r--. 1 1107 1107 1441 Nov 7 06:04 freebsd
-rw-r--r--. 1 1107 1107 3526 Nov 7 06:04 linux
drwxrwxrwx. 2 1107 1107 84 Nov 7 06:17 macos
[root@postgresql-server start-scripts]#
Linux文件即为Linux系统上的启动脚本,该脚本是Init 风格的。
创建 Systemd 风格的服务脚本:
cat > /etc/systemd/system/postgresql.service <<"EOF"
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGPORT=5432
Environment=PGDATA=/opt/postgresql/pgdata
OOMScoreAdjust=-1000
ExecStart=/opt/postgresql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/opt/postgresql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/opt/postgresql/bin/pg_ctl reload -D ${PGDATA} -s
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
4.连接数据库服务
本地连接方式连接pgsql
[root@postgresql-server data]# su - postgres
Last login: Wed Nov 15 16:31:31 CST 2023 on pts/1
[postgres@postgresql-server ~]$ psql
psql (16.1)
Type "help" for help.
postgres=#
远程访问
修改pg_hba.conf
[root@postgresql-server data]# cat pg_hba.conf
# ----------------------------------
#
# 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.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
参数作用:
- TYPE:连接类型一般有 local 和 host 两种,local 指的是本地连接,host 则类似外部的ssh远程服务器的访问方式。
- DATABASE:表示要连接的数据库,all 表示所有。这里可以改为具体的数据库名称
- USER:这里填写Linux用户。比如默认分配的postgres可以免密访问。
- ADDRESS:127.0.0.1/32表示只允许来自己本机的连接,0.0.0.0/0 表示所有的Ip放行。192.168.1.0/24表示允许192.168.1.1-192.168.1.255这个地址段的ip地址连接。
- METHOD:表示连接的认证方式,PostgreSQL的连接命令psql有两种连接方式:
- 不带-h参数或host参数时,是local连接,用的是peer认证方式。
- 使用-h localhost、-h 127.0.0.1、postgres@localhost 或 postgres@127.0.0.1 这样的格式,则会使用host类型,使用TCP/IP的方式连接,使用的是ident的认证方式。
为了实现外部远程访问,配置做如下修改:
host all all 0.0.0.0/0 trust
pg_ident.conf
数据库映射文件,ident 认证方式的扩展,标注操作系统用户与数据库用户的映射关系,配合pg_hba.conf 使用。允许数据库服务器上指定的操作系统用户,使用指定的数据库用户,免密连入数据库。
pg_ident.conf 文件
# MAPNAME SYSTEM-USERNAME PG-USERNAME
ss aaa test
ss syd syd
- MAPNAME:映射名,自定义配置在 pg_hba.conf 文件中。
- SYSTEM-USERNAME:系统用户名。
- PG-USERNAME :数据库用户名。
pg_hba.conf 文件
# TYPE DATABASE USER ADDRESS METHOD
local all all md5
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 idnet map=ss
host all all ::1/128 md5
local replication all md5
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
重新加载即可生效
pg_ctl reload
重启postgresql之后我们继续执行psql,发现报错还是存在,root还是无法访问:
[root@postgresql-server data]# psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
[root@postgresql-server data]#
修改 postgresql.conf 文件
通过vim /var/lib/psql/pg版本号/postgresql.conf ,我们找到下面的内容,把listen_addresses的注释放开,并且把内容修改为*,然后wq保存退出即可。
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
[root@postgresql-server data]# psql -h 192.168.10.44 -U postgres
psql (16.1)
Type "help" for help.
postgres=#
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "192.168.10.44" at port "5432".
postgres=#




