PostgreSQL 16.2安装

发布时间 2023-07-07 17:29:44作者: junyue

1、postgresql安装-软件包安装

 

1.1下载安装包,官网下载地址:https://www.postgresql.org/download/

 

如果服务器有网络,可以选择yum安装。

在服务器没有网络的情况下,需要先下载rpm包,我们选择这种方式。

 

现在最新版本是PostgreSQL 16.2,

2023-06-29: postgresql16-16-beta2_1PGDG.rhel7

 

里面有4个rpm包,必须下载3个,postgresql16 客户端, postgresql16-libs 库文件, postgresql16-server 数据库服务端

postgresql16-contrib 额外的插件模块(可不安装)

 

 

1.2安装步骤

rpm –ivh postgresql16-libs-16-beta2_1PGDG.rhel7.x86_64.rpm

rpm –ivh postgresql16-16-beta2_1PGDG.rhel7.x86_64.rpm

rpm –ivh postgresql16-server-16-beta2_1PGDG.rhel7.x86_64.rpm

 

默认安装程序目录:

/usr/pgsql-16/bin

默认数据目录:

/var/lib/pgsql/16/data

默认servicename目录

/usr/lib/systemd/system/postgresql-16.service

 

配置path目录

export PATH=$PATH:/usr/pgsql-16/bin

 

初始化数据库

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

创建数据目录、servicename、操作系统账号。

 

启动数据库

systemctl enable postgresql-16

systemctl start postgresql-16

关闭数据库

systemctl stop postgresql-16

 

登录测试(免密码)

Su postgres

psql  postgres

 

修改默认用户密码 postgres

su  - postgres -c "psql"

语句1:\password postgres

语句2:ALTER USER postgres PASSWORD ''

 

测试密码登录

psql -h localhost -p 5432 postgres –U postgres

 

测试sql语句

create table tb1 (aaa text,bbb numeric);

insert into tb1 values('good',123);

select *From tb1;

 

----------

删除数据库

/usr/pgsql-16/bin/dropdb postgres

 

卸载rpm

Rpm –qa |grep postgres

rpm –e postgresql16-libs-16-beta2_1PGDG.rhel7.x86_64.rpm

rpm –e postgresql16-16-beta2_1PGDG.rhel7.x86_64.rpm

rpm –e postgresql16-server-16-beta2_1PGDG.rhel7.x86_64.rpm

 

检查是否删除

/usr/pgsql-16/bin/dropdb -p 5432 -h localhost -i -e postgres

 

 

which initdb

 

创建数据库目录

 

修改目录属性

chmod 700 pgdata_st2/

 

初始化一个数据库

initdb -D /usr/local/pgsql/data

 

启动数据库

pg_ctl start -l logfile

 

 

ALTER USER pgtest PASSWORD 'Pg_123456'

 

安装默认创建了3个数据库

SELECT datname FROM pg_database;

 

 

2、postgresql安装-源代码安装

最新版本是v16.2  https://www.postgresql.org/ftp/source/v16beta2/

 

 

 

 

下载源代码包,postgresql-16beta2.tar.gz

 

 

解压

tar -xzvf

 

编译配置

./configure

./configure --prefix=PREFIX

把所有文件装在目录PREFIX中而不是/usr/local/pgsql中

 

编译

make

make all

 

安装

Sudo make install

 

 

默认时所有文件都将安装到/usr/local/pgsql。

通常也就是/usr/local/pgsql/bin。

 

安装后配置环境变量

LD_LIBRARY_PATH=/usr/local/pgsql/lib

export LD_LIBRARY_PATH

 

PATH=/usr/local/pgsql/bin:$PATH

export PATH

 

 

初始化

initdb -D /home/pgtest/pgdata16

 

启动数据库

[pgtest@host1 pgdata16]$ pg_ctl -D /home/pgtest/pgdata16 -l log1.log start

 

 

关闭数据库

pg_ctl stop -D

 

登录测试(免密码)

Su postgres

Psql postgres

psql -h localhost -p 5432 postgres

 

 

 

 

异常问题处理:

1、icu编译报错问题

configure: error: ICU library not found

If you have ICU already installed, see config.log for details on the

failure.  It is possible the compiler isn't looking in the proper directory.

Use --without-icu to disable ICU support.

 

configure: error: readline library not found

If you have readline already installed, see config.log for details on the

failure.  It is possible the compiler isn't looking in the proper directory.

Use --without-readline to disable readline support.

 

解决办法,yum搜索,需要devel版本的包,安装。

yum search all icu

yum install libicu-devel.x86_64

安装后检查头文件是否存在了

ls -l /usr/include/ | grep zlib

 

./configure --without-icu --without-readline

 

pg16.2编译通过。