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:lhr@192.168.100.100: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
相关推荐
尘浮生22 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
Code_Artist35 分钟前
使用Portainer来管理并编排Docker容器
docker·云原生·容器
六月闻君35 分钟前
MySQL 报错:1137 - Can‘t reopen table
数据库·mysql
mengao123436 分钟前
centos 服务器 docker 使用代理
服务器·docker·centos
Eternal-Student43 分钟前
【docker 保存】将Docker镜像保存为一个离线的tar归档文件
运维·docker·容器
SelectDB技术团队44 分钟前
兼顾高性能与低成本,浅析 Apache Doris 异步物化视图原理及典型场景
大数据·数据库·数据仓库·数据分析·doris
不是二师兄的八戒1 小时前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
码农小丘1 小时前
一篇保姆式centos/ubuntu安装docker
运维·docker·容器
inventecsh1 小时前
mongodb基础操作
数据库·mongodb
白云如幻1 小时前
SQL99版链接查询语法
数据库·sql·mysql