PostgreSQL的离线安装及问题解决
作者:鱼仔
博客首页: codeease.top (opens new window)
公众号:神秘的鱼仔
# PostgreSQL安装方式
PostgreSQL的安装主要分为二进制安装和源码安装,生产环境往往需要在Linux非Root权限离线安装,因此本博客采用源码离线安装的方式安装PostgreSQL。
安装过程参考官方文档:https://www.postgresql.org/docs/17/installation.html
# 下载PostgreSQL
第一步下载PostgreSQL的源码包,可以直接在下面这个目录找到对应的文件:
https://www.postgresql.org/ftp/source/ (opens new window)
我选择的是postgresql-17.5.tar.gz
然后上传到Linux服务器中,目录可以自己定,我放在了/usr/local/software中
# 安装PostgreSQL(使用非Root权限安装)
# 1、创建必备目录
cd /usr/local/
mkdir postgresql
cd postgresql
mkdir install
mkdir data
2
3
4
5
# 2、执行安装
cd /usr/local/software/
tar -xzf postgresql-17.5.tar.gz
cd postgresql-17.5
./configure --prefix=/usr/local/postgresql/install/
make
make install
2
3
4
5
6
如果没有报错的话即表示安装完成,如果中途遇到报错,可以看最后一章查看解决方案
# 3、初始化并启动数据库
这里的用户名是超级用户,请将test修改成自己的用户名
/usr/local/postgresql/install/bin/initdb -D /usr/local/postgresql/data/ --locale=zh_CN.UTF-8 --encoding=UTF8 -U test
/usr/local/postgresql/install/bin/pg_ctl -D /usr/local/postgresql/data/ -l logfile start
2
# 4、创建数据库、创建用户
执行下面的命令可以进入到PGSQL的控制台中
/usr/local/postgresql/install/bin/psql -U test -d postgres
创建一个名叫user_test的用户
CREATE USER user_test WITH PASSWORD 'user_test';
创建一个名叫test_database的数据库
CREATE DATABASE test_database;
将test_database的所有权限赋给user_test用户
GRANT ALL PRIVILEGES ON DATABASE test_database TO user_test;
# 5、配置环境变量
将PostgreSQL的配置放到环境变量中
vi ~/.bashrc
将下面这段配置放到末尾
export LD_LIBRARY_PATH=/usr/local/postgresql/install/lib/:$LD_LIBRARY_PATH
刷新配置
source ~/.bashrc
配置客户端认证规则,允许任何IP连接到PostgreSQL,通过md5密码校验
vi /usr/local/postgresql/data/pg_hba.conf
在最后加上这样一句
host all all 0.0.0.0/0 md5
配置监听所有的网络接口
vi /usr/local/postgresql/data/postgresql.conf
在最后添加下面的配置
listen_addresses = '*'
# 6、重启服务
/usr/local/postgresql/install/bin/pg_ctl -D /usr/local/postgresql/data/ -l logfile restart
# 7、客户端连接
主流的数据库客户端工具都可以连接PostgreSQL,我个人使用Navicat进行连接
# 可能会遇到的问题
1、GCC未安装
checking for gcc... no
checking for cc... no
configure: error: in `/usr/local/software/postgresql-17.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
2
3
4
5
遇到这种情况可以运行 gcc --version 查看是否有gcc,没有的话补上就可以。yum源有的话直接通过 yum install gcc 安装
2、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.
2
3
4
这种情况是ICU没有安装,这是一个国际化库(International Components for Unicode)解决办法有两种,在configure命令后跟上 --without-icu 禁用国际化支持,或者安装ICU,安装命令:
yum install libicu-develœ
3、bison未安装
checking for bison... no
configure: error: bison not found
2
解决方案:
yum install bison
4、flex未安装
checking for flex... no
configure: error: flex not found
2
解决方案
yum install flex
5、readline未找到
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.
2
3
4
这个库主要用于提供命令行编辑功能,比如上下箭头翻阅历史命令,Tab补全,因为PostgreSQL的psql命令行工具会依赖 Readline 来增强功能。两种方式,第一种在configure命令后跟上 --without-readline 参数,第二种方案安装对应的依赖:
yum install readline-devel
6、zlib未找到
checking for inflate in -lz... no
configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.
2
3
4
5
zlib库用于数据压缩,比如日志压缩功能。两种方式,第一种在configure命令后跟上--without-zlib ,第二种方式安装对应的依赖
yum install zlib-devel
# 总结
上面的问题基本上覆盖了安装会遇到的问题,如果出现一些更基础的问题,比如make没有安装等等,对应安装完即可。
参考文档:
https://www.postgresql.org/docs/17/installation.html