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
相关推荐
MonkeyKing_sunyuhua40 分钟前
ubuntu22.04 docker-compose安装postgresql数据库
数据库·docker·postgresql
天郁青40 分钟前
数据库交互的本地项目:后台管理系统
数据库·交互
马剑威(威哥爱编程)1 小时前
MongoDB面试专题33道解析
数据库·mongodb·面试
小光学长1 小时前
基于vue框架的的流浪宠物救助系统25128(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
数据库·vue.js·宠物
追风林1 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
零炻大礼包2 小时前
【SQL server】数据库远程连接配置
数据库
zmgst2 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
随心............2 小时前
python操作MySQL以及SQL综合案例
数据库·mysql
€☞扫地僧☜€2 小时前
docker 拉取MySQL8.0镜像以及安装
运维·数据库·docker·容器
茶馆大橘2 小时前
微服务系列六:分布式事务与seata
分布式·docker·微服务·nacos·seata·springcloud