Linux环境下PG 14的安装部署

网友投稿 807 2022-05-30

安装 PostgreSQL 14

常用方法

下载PG

https://www.postgresql.org/

https://yum.postgresql.org/rpmchart.php

https://yum.postgresql.org/14/redhat/rhel-7-x86_64/repoview/postgresqldbserver14.group.html

https://www.postgresql.org/ftp/source/

打开 PostgreSQL 官网 https://www.postgresql.org/,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 linux、Windows、Mac OS等 ;也可以直接访问:https://www.postgresql.org/download/ 进行下载。

yum在线安装

https://www.postgresql.org/download

https://yum.postgresql.org/repopackages.php

文档:https://www.postgresql.org/download/linux/redhat/

rpm下载:https://yum.postgresql.org/rpmchart/

docker rm -f lhrpgccc docker run -d --name lhrpgccc -h lhrpgccc \ -p 25432-25439:5432-5439 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.8 \ /usr/sbin/init docker exec -it lhrpgccc bash -- 一些依赖包 yum install -y cmake make gcc zlib zlib-devel gcc-c++ perl readline readline-devel \ python36 tcl openssl ncurses-devel openldap pam flex -- 删除已存在的PG yum remove -y postgresql* && rm -rf /var/lib/pgsql && rm -rf /usr/pgsql* && userdel -r postgres && groupdel postgres yum install -y sysbench -- 安装yum源 yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm yum repolist all | grep pgdg yum repolist enabled | grep pgdg -- 安装pg,注意:postgresql14-contrib是安装插件的 yum install -y postgresql14 postgresql14-server postgresql14-libs postgresql14-contrib -- 验证 [root@lhrpgccc /]# rpm -aq| grep postgres postgresql14-14.2-1PGDG.rhel7.x86_64 postgresql14-contrib-14.2-1PGDG.rhel7.x86_64 postgresql14-libs-14.2-1PGDG.rhel7.x86_64 postgresql14-server-14.2-1PGDG.rhel7.x86_64 -- 环境变量 echo "export PATH=/usr/pgsql-14/bin:$PATH" >> /etc/profile -- 初始化PG /usr/pgsql-14/bin/postgresql-14-setup initdb systemctl enable postgresql-14 systemctl start postgresql-14 systemctl status postgresql-14 -- 本地登陆 su - postgres psql -- 安装插件 create extension pageinspect; create extension pg_stat_statements; select * from pg_extension ; select * from pg_available_extensions order by name; -- 修改postgres密码 alter user postgres with encrypted password 'lhr'; 或 \password select * from pg_tables limit 10; select version(); firewall-cmd --add-port=5432/tcp --permanent firewall-cmd --reload firewall-cmd --list-port -- 配置允许PG远程登录,注意版本: cat >> /var/lib/pgsql/14/data/postgresql.conf <<"EOF" listen_addresses = '*' port=5432 unix_socket_directories='/var/lib/pgsql/14/data' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%a.log' log_truncate_on_rotation = on EOF cat > /var/lib/pgsql/14/data/pg_hba.conf << EOF # TYPE DATABASE USER ADDRESS METHOD host all all 0.0.0.0/0 trust EOF systemctl restart postgresql-14 -- 远程登陆 psql -U postgres -h 192.168.66.35 -d postgres -p25432 -- 从Postgresql 9.2开始,还可以使用URI格式进行连接:psql postgresql://myuser:mypasswd@myhost:5432/mydb psql postgresql://postgres:lhr@192.168.66.35:25432/postgres \l CREATE DATABASE lhrdb WITH OWNER=postgres ENCODING='UTF-8'; \c lhrdb create table student ( id integer not null, name character(32), number char(5), constraint student_pkey primary key (id) ); \d student INSERT INTO student (id, name, number) VALUES (1, '张三', '1023'); SELECT * FROM student WHERE id=1;

其中-h参数指定服务器地址,默认为127.0.0.1,默认不指定即可,-d指定连接之后选中的数据库,默认也是postgres,-U指定用户,默认是当前用户,-p 指定端口号,默认是"5432",其它更多的参数选项可以执行: ./bin/psql --help 查看

docker安装

Docker Hub的官网地址:https://hub.docker.com/_/postgres

GitHub的地址:https://github.com/docker-library/postgres

Linux环境下PG 14的安装部署

-- 拉取所有镜像 docker pull postgres:9.4.26 docker pull postgres:9.6.24 docker pull postgres:10.20 docker pull postgres:11.15 docker pull postgres:12.10 docker pull postgres:13.6 docker pull postgres:14.2 -- 创建各个版本的Docker容器 docker rm -f lhrpg94 lhrpg96 lhrpg10 lhrpg11 lhrpg12 lhrpg13 lhrpg14 docker run --name lhrpg94 -h lhrpg94 -d -p 54321:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:9.4.26 docker run --name lhrpg96 -h lhrpg96 -d -p 54322:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:9.6.24 docker run --name lhrpg10 -h lhrpg10 -d -p 54323:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:10.20 docker run --name lhrpg11 -h lhrpg11 -d -p 54324:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:11.15 docker run --name lhrpg12 -h lhrpg12 -d -p 54325:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:12.10 docker run --name lhrpg13 -h lhrpg13 -d -p 54326:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:13.6 docker run --name lhrpg14 -h lhrpg14 -d -p 54327:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:14.2 docker exec -it lhrpg14 psql -U postgres -d postgres select * from pg_tables ; select version(); psql -U postgres -h 172.17.0.12 -d postgres psql -U postgres -h 192.168.66.35 -p 54324 -d postgres

二进制安装

https://www.enterprisedb.com/download-postgresql-binaries

从11开始,不再支持Linux版本的二进制,但是Windows依然支持。

解压,并且配置环境变量即可使用:

cat >> /root/.bashrc <<"EOF" export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH export PATH=$PATH:/usr/local/pgsql/bin EOF

注意:psql和gsql的lib库不兼容,参考:https://www.xmmup.com/guzhangchulipsqlkehuduanlianjiepgbaocuopsql-error-expected-authentication-request-from-serv.html

源码安装(生产库建议源码安装)

https://www.postgresql.org/ftp/source/

http://postgres.cn/docs/13/install-procedure.html

以下方法已在9.6到14版本测试过:

-- 下载源码包 wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz --no-check-certificate wget https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.gz wget https://ftp.postgresql.org/pub/source/v12.7/postgresql-12.7.tar.gz wget https://ftp.postgresql.org/pub/source/v11.12/postgresql-11.12.tar.gz wget https://ftp.postgresql.org/pub/source/v10.17/postgresql-10.17.tar.gz wget https://ftp.postgresql.org/pub/source/v9.6.22/postgresql-9.6.22.tar.gz wget https://ftp.postgresql.org/pub/source/v9.4.26/postgresql-9.4.26.tar.gz -- 一些依赖包 yum install -y cmake make gcc zlib zlib-devel gcc-c++ perl readline readline-devel \ python36 tcl openssl ncurses-devel openldap pam flex -- 创建用户 useradd pgsql echo "lhr" | passwd --stdin pgsql -- 创建目录 mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg14,soft} chown -R pgsql:pgsql /postgresql chmod -R 775 /postgresql -- 编译 su - pgsql cd /postgresql/soft tar zxvf postgresql-14.2.tar.gz cd postgresql-14.2 ./configure --prefix=/postgresql/pg14 make -j 16 && make install # 编译完成,最后一行显示:All of PostgreSQL successfully made. Ready to install. -- 如果你希望编译所有能编译的东西,包括文档(HTML和手册页)以及附加模块(contrib),这样键入: make world -j 16 && make install-world #最后一行显示:PostgreSQL, contrib, and documentation successfully made. Ready to install. -- 源码安装postgresql时,而make时又没有make world,就会导致的pg最终没有类似pg_stat_statements的扩展功能 -- 配置环境变量 cat >> ~/.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 ~/.bash_profile -- 初始化 su - pgsql /postgresql/pg14/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres -- 修改参数 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 host all all 0.0.0.0/0 md5 EOF -- 启动 su - pgsql pg_ctl start pg_ctl status pg_ctl stop -- 配置系统服务 cat > /etc/systemd/system/PG14.service <<"EOF" [Unit] Description=PostgreSQL database server Documentation=man:postgres(1) After=network.target [Service] Type=forking User=pgsql Group=pgsql Environment=PGPORT=5433 Environment=PGDATA=/postgresql/pgdata OOMScoreAdjust=-1000 ExecStart=/postgresql/pg14/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300 ExecStop=/postgresql/pg14/bin/pg_ctl stop -D ${PGDATA} -s -m fast ExecReload=/postgresql/pg14/bin/pg_ctl reload -D ${PGDATA} -s KillMode=mixed KillSignal=SIGINT TimeoutSec=0 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable PG14 systemctl start PG14 systemctl status PG14 su - pgsql psql \password postgres or: alter user postgres with password 'lhr'; -- 安装插件 create extension pageinspect; create extension pg_stat_statements; select * from pg_extension ; select * from pg_available_extensions order by name;

编译参数介绍:

–with-pgport=NUMBER

把NUMBER设置为服务器和客户端的默认端口。默认是 5432。 这个端口可以在以后修改,不过如果你在这里声明,那么服务器和客户端将有相同的编译好了的默认值。这样会非常方便些。 通常选取一个非默认值的理由是你企图在同一台机器上运行多个PostgreSQL服务器。

–with-openssl

编译SSL(加密)连接支持。这个选项需要安装OpenSSL包。configure将会检查所需的头文件和库以确保你的 OpenSSL安装足以让配置继续下去。

–with-perl

制作PL/Perl服务器端编程语言。

–with-python

制作PL/Python服务器端编程语言。

–with-blocksize=BLOCKSIZE

设置块尺寸,以 K 字节计。这是表内存储和I/O的单位。默认值(8K字节)适合于大多数情况,但是在特殊情况下可能其他值更有用。这个值必须是2的幂并且在 1 和 32 (K字节)之间。注意修改这个值需要一次 initdb。

–with-systemd

系统服务方式管理

更多编译选项请参考:http://postgres.cn/docs/13/install-procedure.html

配置环境变量

echo "export PATH=/usr/pgsql-14/bin:$PATH" >> /etc/profile mkdir -p /home/postgres chown postgres.postgres /home/postgres -R sed -i 's|/var/lib/pgsql|/home/postgres|' /etc/passwd echo "lhr" |passwd --stdin postgres cat > /home/postgres/.bash_profile <<"EOF" export PGPORT=5432 export PGHOME=/usr/pgsql-14 export PGDATA=/var/lib/pgsql/14/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]\$ " EOF chown postgres.postgres /home/postgres/.bash_profile

系统参数修改

systemctl status firewalld.service systemctl stop firewalld.service systemctl disable firewalld.service setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config cat > /etc/sysctl.conf <<"EOF" vm.swappiness=10 vm.zone_reclaim_mode=0 fs.aio-max-nr = 1048576 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.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

Linux

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:2021年大数据Spark(二十五):SparkSQL的RDD、DF、DS相关操作
下一篇:基于STM32+华为云IOT设计的酒驾监控系统【玩转华为云】
相关文章