PostgreSQL 16数据库的yum、编译、docker三种方式安装——筑梦之路

一、 docker方式

bash 复制代码
docker pull postgres:16.0

docker rm -f lhrpg16

docker run --name lhrpg16 -h lhrpg16 -d -p 54329:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:16.0

docker exec -it lhrpg16 bash

docker exec -it lhrpg16 psql -U postgres -d postgres

select * from pg_tables;
select version();

二、yum方式

bash 复制代码
dnf install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel \
    perl python36 tcl openssl ncurses-devel openldap pam  perl-IPC-Run libicu-devel

dnf install epel-release -y

dnf --enablerepo=powertools install perl-IPC-Run -y

# 自2023年8月起,PostgreSQL RPM repo停止向PostgreSQL RPM repo添加新包,**包括PostgreSQL 16**.我们将维护旧的主要版本,直到每个主要版本被PostgreSQL项目终止。请访问这里每个主要版本的最新发布日期。https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/

wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/postgresql16-16.0-1PGDG.rhel8.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/postgresql16-server-16.0-1PGDG.rhel8.x86_64.rpm

wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/postgresql16-contrib-16.0-1PGDG.rhel8.x86_64.rpm

wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/postgis34_16-devel-3.4.0-1PGDG.rhel8.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/postgresql16-libs-16.0-1PGDG.rhel8.x86_64.rpm

# 安装

dnf localinstall postgresql16-*

# (建议安装)安装开发包,若后期需要编译一些插件,例如pg_recovery、pg_dirtyread等都需要该包

dnf localinstall postgresql16-devel-16.0-1PGDG.rhel8.x86_64.rpm

rpm -aq| grep postgres

echo "export PATH=/usr/pgsql-16/bin:$PATH" >> /etc/profile

# 初始化

/usr/pgsql-16/bin/postgresql-16-setup initdb

systemctl enable postgresql-16 --now

# 本地登陆

su - postgres

psql

# 安装插件

create extension pageinspect;

create extension pg_stat_statements;

select * from pg_extension;

select * from pg_available_extensions order by name;

# 修改密码

alter user postgres with  password 'lhr'; 或 \password

select * from pg_tables;

select version();

# 配置允许远程登陆

cat >> /var/lib/pgsql/16/data/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/var/lib/pgsql/16/data'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF

cat  << EOF > /var/lib/pgsql/16/data/pg_hba.conf
 # TYPE  DATABASE    USER    ADDRESS       METHOD
 local     all       all                    trust
 host      all       all    ::1/128         trust
 host      all       all   127.0.0.1/32     trust
 host      all       all    0.0.0.0/0        md5
 host   replication  all    0.0.0.0/0        md5
 EOF

systemctl restart postgresql-16

psql -U postgres -h 192.168.100.100 -d postgres -p54327

# 从Postgresql 9.2开始,还可以使用URI格式进行远程连接:psql postgresql://myuser:mypasswd@myhost:5432/mydb

psql postgresql://postgres:[email protected]:54327/postgres

mkdir -p /home/postgres

chown postgres.postgres /home/

sed -i 's|/var/lib/pgsql|/home/postgres|' /etc/passwd

echo "lhr" |passwd --stdin postgres

cat > /home/postgres/.bash_profile <<"EOF"
export PGPORT=54327
export PGHOME=/usr/pgsql-16
export PGDATA=/var/lib/pgsql/16/data
export PATH=$PGHOME/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
export LANG=en_US.UTF-8
export DATE='date +"%Y%m%d%H%M"'
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGPASSWORD=lhr
export PGDATABASE=postgres
export PS1="[\u@\h \W]\$ "
alias ll='ls -l'
EOF


chown postgres.postgres /home/postgres/.bash_profile

三、编译方式

bash 复制代码
# 下载源码

wget https://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gz

# 创建用户

groupadd -g 60000 pgsql

useradd -u 60000 -g pgsql pgsql

echo "lhr" | passwd --stdin pgsql

# 创建目录

mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg16,soft}

chown -R pgsql:pgsql /postgresql

chmod -R 775 /postgresql

# 安装依赖包

yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel 
    perl python36 tcl openssl ncurses-devel openldap pam perl-IPC-Run libicu-devel

# 编译源码

su - pgsql

cd /postgresql/soft

tar zxvf postgresql-16.0.tar.gz

cd postgresql-16.0

./configure --prefix=/postgresql/pg16

make -j 8 && make install

make world -j 8 && make install-world

# 配置环境变量

cat >>  ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg16
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
EOF

source  ~/.bash_profile

# 初始化数据库

su - pgsql

/postgresql/pg16/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres --data-checksums

# 调整参数

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

cat   >> /postgresql/pgdata/pg_hba.conf << EOF
# TYPE  DATABASE    USER    ADDRESS       METHOD
local     all       all                    trust
host      all       all   127.0.0.1/32     trust
host      all       all    0.0.0.0/0        md5
host   replication  all    0.0.0.0/0        md5
EOF

# 启动服务

su - pgsql

pg_ctl start

pg_ctl status

pg_ctl stop


# 修改数据库超管密码

pg_ctl start 

psql

alter user postgres with  password 'lhr';

exit

# 注册服务到systemd

cat > /etc/systemd/system/PG16.service <<"EOF"
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network.target

[Service]
Type=forking
User=pgsql
Group=pgsql
Environment=PGPORT=5432
Environment=PGDATA=/postgresql/pgdata
OOMScoreAdjust=-1000
ExecStart=/postgresql/pg16/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/postgresql/pg16/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/postgresql/pg16/bin/pg_ctl reload -D ${PGDATA} -s
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable PG16 --now
systemctl start PG16
systemctl status PG16

随手一记:生成密码的密文sha256加密

bash 复制代码
echo -n "Enter Password: " && head -1 </dev/stdin | tr -d '\n' | sha256sum | cut -d" " -f1
相关推荐
邴越几秒前
不同向量数据库(FAISS / Pinecone / Weaviate)在 RAG 中的优缺点
数据库·faiss
Allen Bright1 分钟前
【MySQL基础-21】MySQL事务机制详解:原理、实现与最佳实践
数据库·mysql
movie__movie16 分钟前
Spring AI MCP 客户端实战:轻松连接高德地图等工具
数据库·人工智能·spring
清风198118 分钟前
kafka消息可靠性传输语义
数据库·分布式·kafka
Ares-Wang31 分钟前
Docker Compose 部署Nginx反向代理 tomcat
nginx·docker·tomcat
基哥的奋斗历程42 分钟前
kubernetes configMap 存储
云原生·容器·kubernetes
数据智能老司机3 小时前
CockroachDB权威指南——SQL调优
数据库·分布式·架构
数据智能老司机3 小时前
CockroachDB权威指南——应用设计与实现
数据库·分布式·架构
数据智能老司机3 小时前
CockroachDB权威指南——CockroachDB 模式设计
数据库·分布式·架构
数据智能老司机21 小时前
CockroachDB权威指南——CockroachDB SQL
数据库·分布式·架构