添加用户
sudo useradd postg // 添加用户
sudo passwd postg // 添加密码
创建postgresql 目录
mkdir /opt/postgresql-16.0 // 创建部署目录
mkdir /opt/postgresql-16.0/data // 创建数据目录
touch /opt/postgresql-16.0/pgsql.log // 创建日志文件
// 文件夹让刚刚创建的用户具有权限
chown -R postg:postg /postgresql-16.0
chmod -R 775 /postgresql-16.0
sudo chown postg:postg /postgresql-16.0 // 确保是这个账户的拥有者
sudo chown postg:postg /postgresql-16.0/pgsql.log // 确保是这个账户的拥有者
使用postg
账户
su - postg // 以下所有步骤都使用这个账户操作
下载好 postgreSQL安装包
https://ftp.postgresql.org/pub/source/ // 下载地址,选择好下载的安装包
解压 postgreSQL
tar zxvf postgresql-16.0.tar.gz
安装 gcc
yum install gcc
安装 zlib-devel
yum install zlib-devel
指定安装到 /opt/postgresql-16.2 目录
./configure --prefix=/opt/postgresql-16.2/ --without-readline
提示缺少 ICU,安装ICU
sudo yum install -y libicu
查看是否安装成功
ls /usr/lib64/libicu*
// 会输出下面的信息
/usr/lib64/libicudata.so.60 /usr/lib64/libicutest.so.60
/usr/lib64/libicudata.so.60.3 /usr/lib64/libicutest.so.60.3
/usr/lib64/libicui18n.so.60 /usr/lib64/libicutu.so.60
/usr/lib64/libicui18n.so.60.3 /usr/lib64/libicutu.so.60.3
/usr/lib64/libicuio.so.60 /usr/lib64/libicuuc.so.60
/usr/lib64/libicuio.so.60.3 /usr/lib64/libicuuc.so.60.3
在继续安装,又缺少文件,以下是错误文件
./configure --prefix=/opt/postgresql-16.2/ --without-readline
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for gcc option to accept ISO C99... none needed
checking for g++... no
checking for c++... no
checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking for gawk... gawk
checking whether gcc supports -Wdeclaration-after-statement, for CFLAGS... yes
checking whether gcc supports -Werror=vla, for CFLAGS... yes
checking whether gcc supports -Werror=unguarded-availability-new, for CFLAGS... no
checking whether g++ supports -Werror=unguarded-availability-new, for CXXFLAGS... no
checking whether gcc supports -Wendif-labels, for CFLAGS... yes
checking whether g++ supports -Wendif-labels, for CXXFLAGS... no
checking whether gcc supports -Wmissing-format-attribute, for CFLAGS... yes
checking whether g++ supports -Wmissing-format-attribute, for CXXFLAGS... no
checking whether gcc supports -Wimplicit-fallthrough=3, for CFLAGS... yes
checking whether g++ supports -Wimplicit-fallthrough=3, for CXXFLAGS... no
checking whether gcc supports -Wcast-function-type, for CFLAGS... yes
checking whether g++ supports -Wcast-function-type, for CXXFLAGS... no
checking whether gcc supports -Wshadow=compatible-local, for CFLAGS... yes
checking whether g++ supports -Wshadow=compatible-local, for CXXFLAGS... no
checking whether gcc supports -Wformat-security, for CFLAGS... yes
checking whether g++ supports -Wformat-security, for CXXFLAGS... no
checking whether gcc supports -fno-strict-aliasing, for CFLAGS... yes
checking whether g++ supports -fno-strict-aliasing, for CXXFLAGS... no
checking whether gcc supports -fwrapv, for CFLAGS... yes
checking whether g++ supports -fwrapv, for CXXFLAGS... no
checking whether gcc supports -fexcess-precision=standard, for CFLAGS... yes
checking whether g++ supports -fexcess-precision=standard, for CXXFLAGS... no
checking whether gcc supports -funroll-loops, for CFLAGS_UNROLL_LOOPS... yes
checking whether gcc supports -ftree-vectorize, for CFLAGS_VECTORIZE... yes
checking whether gcc supports -Wunused-command-line-argument, for NOT_THE_CFLAGS... no
checking whether gcc supports -Wcompound-token-split-by-macro, for NOT_THE_CFLAGS... no
checking whether gcc supports -Wformat-truncation, for NOT_THE_CFLAGS... yes
checking whether gcc supports -Wstringop-truncation, for NOT_THE_CFLAGS... yes
checking whether gcc supports -Wcast-function-type-strict, for NOT_THE_CFLAGS... no
checking whether gcc supports -fvisibility=hidden, for CFLAGS_SL_MODULE... yes
checking whether g++ supports -fvisibility=hidden, for CXXFLAGS_SL_MODULE... no
checking whether g++ supports -fvisibility-inlines-hidden, for CXXFLAGS_SL_MODULE... no
checking whether the C compiler still works... yes
checking how to run the C preprocessor... gcc -E
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking allow thread-safe client libraries... yes
checking whether to build with ICU support... yes
checking for icu-uc icu-i18n... no
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.
原因是刚刚配置了,没有安装环境变量
export ICU_CFLAGS="-I/usr/include"
export ICU_LIBS="-L/usr/lib64 -licuuc -licui18n"
然后继续在安装,又出错了
checking whether preadv is declared... yes
checking whether pwritev is declared... yes
checking whether F_FULLFSYNC is declared... no
checking for explicit_bzero... yes
checking for getopt... yes
checking for getpeereid... no
checking for inet_aton... yes
checking for mkdtemp... yes
checking for strlcat... no
checking for strlcpy... no
checking for strnlen... yes
checking for pthread_barrier_wait... yes
checking for getopt_long... yes
checking for syslog... yes
checking syslog.h usability... yes
checking syslog.h presence... yes
checking for syslog.h... yes
checking for opterr... yes
checking for optreset... no
checking unicode/ucol.h usability... no
checking unicode/ucol.h presence... no
checking for unicode/ucol.h... no
configure: error: header file <unicode/ucol.h> is required for ICU
错误提示表明在配置 PostgreSQL 时,系统找不到 ICU (International Components for Unicode) 库中的 <unicode/ucol.h> 头文件。这个头文件是 ICU 库的一部分,用于支持 Unicode 排序。要解决这个问题,您需要确保 ICU 库已经正确安装,并且其头文件路径被包含在编译器的搜索路径中。
安装 libicu-devel
sudo yum install -y libicu-devel
检查安装
ls /usr/include/unicode/ucol.h
如果文件存在,那么配置脚本应该能够找到它。
// 继续配置环境变量,防止等会找不到
export ICU_CFLAGS="-I/usr/include"
在继续安装,安装成功
接着编译安装
make && make install
配置环境变量
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
alias psql='rlwrap psql'
EOF
// 然后source ~/.bash_profile
source ~/.bash_profile
初始化安装
/postgresql/pg16/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
启动成功
启动postgresql,并指定数据目录和日志文件
cd /postgresql-16.0/bin
./pg_ctl -D /opt/postgresql-16.0/data/ -l /opt/postgresql-16.0/pgsql.log start 启动 // 或者 pg_ctl start
./pg_ctl -D /opt/postgresql-16.0/data/ -l /opt/postgresql-16.0/pgsql.log stop 停止 // 或者 pg_ctl stop
./pg_ctl -D /opt/postgresql-16.0/data/ -l /opt/postgresql-16.0/pgsql.log status 查看状态 // 或者 pg_ctl status
// 如果没有办法使用 pg_ctl ,说明没有添加环境变量,添加到环境变量就可以了
export PATH=/opt/postgresql-16.2/bin:$PATH
ps -aux | grep postgres // 查看进程
配置远程连接
vim /postgresql-16.0/data/pg_hba.conf
// 在末尾添加下面这行
host all all 0.0.0.0/0 md5
// 修改 postgresql.conf
vim /postgresql-16.0/data/postgresql.conf
将第60行的#listen_addresses = 'localhost'改为listen_addresses = '*'
修改默认端口
# 编辑postgresql.conf文件
nano /var/lib/pgsql/data/postgresql.conf
# 找到port这一行并修改
port = 5433
# 重启服务
pg_ctl restart
登录 pgsql
psql -h hostname -p port -U username -d databasename
修改PostgreSQL数据库用户密码
// 登录成功后,开始修改密码
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
SELECT rolname,rolpassword FROM pg_authid; // 查看用户与密码