PostgreSQL 14.3 源码安装调试

摘要:介绍PostgreSQL 14.3 源码安装,postgresql使用和vscode源码调试。

1. 环境准备

1.1 系统参数修改

systemctl status firewalld.service 	#查看防火状态
systemctl stop firewalld.service 	#暂时关闭防火墙
systemctl disable firewalld.service #永久关闭防火墙
 
setenforce 0 	#临时关闭selinux防火墙, setenforce是Linux的selinux防火墙配置命令
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 	#禁用SELinux

cat > /etc/sysctl.conf <<"EOF"
vm.swappiness=10 			#访问交换内存比直接访问物理内存要慢得多, swappiness参数的较低值很可能会提高整体系统性能。
vm.zone_reclaim_mode=0		#关闭zone_reclaim模式,可以从其他zone或NUMA节点回收内存
fs.aio-max-nr = 1048576		#系统中所允许的异步IO请求最大数目
fs.file-max = 6815744		#系统中所允许的文件句柄最大数目
net.ipv4.ip_local_port_range = 9000 65500	#网络连接可用作其源(本地)端口的最小和最大端口的限制
net.core.rmem_default = 262144				#套接字接收缓冲区大小的缺省值
net.core.rmem_max = 4194304					#套接字接收缓冲区大小的最大值
net.core.wmem_default = 262144				#套接字发送缓冲区大小的缺省值
net.core.wmem_max = 1048586					#套接字发送缓冲区大小的最大值
kernel.shmmax = 1288490188					#单个共享内存段的最大值
kernel.shmall = 314572						#可用共享内存的总量,单位是页,一般此值与kernel.shmmax相等
kernel.shmmni = 4096						#单个共享内存段的最小值
kernel.sem = 50100 64128000 50100 1280		#用于控制内核信号量
EOF

sysctl -p

cat >> /etc/security/limits.conf <<"EOF" 
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000
EOF


echo "* - nproc unlimited" > /etc/security/limits.d/90-nproc.conf
echo "session required pam_limits.so" >> /etc/pam.d/login

# 关闭THP
root用户下
在vi /etc/rc.local最后添加如下代码
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

chmod +x /etc/rc.d/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

2. 源码安装

2.1 下载源码包

下载源码包 wget https://ftp.postgresql.org/pub/source/v9.32/postgresql-9.2.tar.gz --no-check-certificate

2.2 安装依赖包

yum install -y cmake make gcc zlib zlib-devel gcc-c++ perl readline readline-devel  \ python36 tcl openssl ncurses-devel openldap pam flex
  • readline (命令行编辑支持库)
  • zlib (数据压缩支持库)
  • flex (词法分析库)
  • bison (语法分析库)

2.3 创建用户

groupadd postgres
useradd postgres
echo "postgres" | passwd --stdin postgres
chown postgres.postgres /home/postgres/.bash_profile

2.4 创建目录

## 创建目录
mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg14,soft}
chown -R postgres:postgres /postgresql
chmod -R 775 /postgresql

​ pgdata:数据目录

​ archive:归档目录

​ backup:WAL目录

​ pg14:安装文件目录

​ soft:源文件目录

2.5 编译安装

-- 编译
su - postgres
cd /postgresql/soft
tar zxvf postgresql-14.3.tar.gz
cd postgresql-14.3
./configure --prefix=/postgresql/pg14
#或增加调试
#./configure --prefix=/postgresql/pg14 --enable-depend --enable-debug --enable-cassert CFLAGS=-O0

make -j 64 && make install

如果希望编译所有能编译的东西,包括文档(HTML和手册页)以及附加模块(contrib),这样键入:
make world -j 16 && make install-world 
sudo /sbin/ldconfig /postgresql/pg14/lib

2.6 配置环境变量

-- 配置环境变量
cat >>  $HOME/.bash_profile <<"EOF"
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg14
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
EOF
source  $HOME/.bash_profile

2.7 初始化数据库

-- 初始化
/postgresql/pg14/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres

2.8 修改参数

-- 修改参数
cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF

2.9 启动数据库

-- 启动
pg_ctl start
pg_ctl status
pg_ctl stop
pg_ctl stop -m fast

3. 连接数据库

su - postgres
psql -p 5432 -h 127.0.0.1;

// 更改数据库密码
alter user postgres with password 'postgres';

4. 安装插件

-- 安装插件
create extension pageinspect;
create extension pg_stat_statements;

select * from pg_extension;
select * from pg_available_extensions order by name;

​ 安装PG数据库时,会建立一个与初始化数据库时的操作系统用户名同名的数据库用户,同时,这个用户是数据库的超级用户,在这OS用户下登录数据库使用的是操作系统认证,所以不需要用户名和密码。也可以通过修改pg_hba.conf文件要求输入密码。

cat   > /postgresql/pgdata/pg_hba.conf << EOF

# TYPE  DATABASE    USER    ADDRESS       METHOD
host      all       all    0.0.0.0/0        md5
EOF

4. 错误处理

4.1 socket链接错误

[postgres@iZuf6ddpzz3ipktm5kj01cZ pgdata]$ psql                                                           
psql: error: connection to server on socket "/postgresql/pgdata/.s.PGSQL.5433" failed: FATAL:  no pg_hba.c
onf entry for host "[local]", user "postgres", database "postgres", no encryption

指定postgresql的ip地址和端口号

psql -p 5433 -h 47.103.xxx.xxx;

4.2 连接postgres服务端忘记密码

[postgres@iZuf6ddpzz3ipktm5kj01cZ ~]$ psql -p 5433 -h 47.103.136.252;
Password for user postgres: 
psql: error: connection to server at "47.103.xxx.xxx", port 5433 failed: FATAL:  password authentication failed for user "postgres"

​ 修改/postgresql/pgdata/pg_hba.conf的METHOD字段为trust

# TYPE  DATABASE    USER    ADDRESS       METHOD
host      all       all    0.0.0.0/0      trust

​ 重新设置密码

postgres=# \password
Enter new password: 
Enter it again: 

重启服务器

pg_ctl reload

5. vscode源码调试

​ 在launch.json中添加新的配置,一共有三个例子,一个postgres --help,一个initdb,一个postgres后端调试。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "postgres --help",
            "type": "cppdbg",
            "request": "launch",
            "program": "/postgresql/pg14/bin/postgres",
            "args": [
                "--help"
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerArgs": "--command=/home/postgres/cmd.gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "initdb",
            "type": "cppdbg",
            "request": "launch",
            "program": "/postgresql/pg14/bin/initdb",
            "args": [
                "-D",
                ""
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerArgs": "--command=/home/postgres/cmd.gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "postgres backend",
            "type": "cppdbg",
            "request": "attach",
            "program": "/postgresql/pg14/bin/postgres",
            "processId": "${command:pickProcess}",
            "stopAtEntry": false,
            "MIMode": "gdb",
            "miDebuggerArgs": "--command=/home/postgres/cmd.gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

​ 创建/home/postgres/cmd.gdb文件,写入文件内容

set print element 0
set print object on
set prin vtbl on
set print pretty on
hand SIGUSR1 SIGUSR2 SIG36 SIGSTOP noprint nostop

​ 在exec_simple_query函数打断点,然后在侧边栏Run and Debug中选择postgres backend,点击调试按钮或者按F5后,输入postgres,选择postgres db xxxx idle进程。

6. 参考

https://cloud.tencent.com/developer/article/2013907

你可能感兴趣的:(数据库,postgresql,数据库)