PostgreSQL 16 编译安装

PostgreSQL About 3,125 words

说明

CentOS 8为例。

安装依赖

libxslt

yum install libxslt-devel

readline

yum install readline-devel

libicuPostgreSQL 16中默认会开启icu对不同语言的字符串进行排序。

yum install libicu-devel

不安装icu会得到以下错误

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.

创建用户

PostgreSQL不能以root用户启动。

添加用户postgres

添加用户叫postgres,是为了使用psql时默认会指定数据库postgres和默认角色postgres,这样psql就可以不用加-d-U参数了。

useradd postgres

用户组添加postgres

groupadd postgres

可能会得到以下告警,忽略即可。

groupadd: group 'postgres' already exists

参看用户

cat /etc/passwd

删除用户

如果想要删除指定用户,可以使用userdel命令。

userdel postgres

出现如下告警,需要退出SSH再进

userdel: user postgres is currently used by process 8243

切换用户进行安装

su postgres

下载源码

从清华大学镜像中下载,选择16.1版本:https://mirrors.tuna.tsinghua.edu.cn/postgresql/source

wget https://mirrors.tuna.tsinghua.edu.cn/postgresql/source/v16.1/postgresql-16.1.tar.gz

解压

tar -zxvf postgresql-16.1.tar.gz

进入目录

cd postgresql-16.1

配置编译参数

./configure --prefix=/home/postgres/16.1 --with-libxml --with-libxslt

编译

make

安装

make install

初始化数据库

创建数据目录

mkdir -p /home/postgres/16.1/data

初始化数据库

/home/postgres/16.1/bin/initdb -D /home/postgres/16.1/data

初始化成功信息

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /home/postgresql/16.1/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /home/postgres/16.1/bin/pg_ctl -D /home/postgres/16.1/data -l logfile start

如果是root用户启动,会得到以下报错

initdb: error: cannot be run as root
initdb: hint: Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.

启动服务

创建日志目录

mkdir -p /home/postgres/16.1/log

启动服务

/home/postgres/16.1/bin/pg_ctl -D /home/postgres/16.1/data -l /home/postgres/16.1/log/pg.log start

停止服务

/home/postgres/16.1/bin/pg_ctl -D /home/postgres/16.1/data -l /home/postgres/16.1/log/pg.log stop

重启服务

/home/postgres/16.1/bin/pg_ctl -D /home/postgres/16.1/data -l /home/postgres/16.1/log/pg.log restart

连接数据库

psql创建软链接

ln -s /home/postgres/16.1/bin/psql /usr/local/bin/psql

直接使用psql即可

psql

root用户连接

psql -d postgres -U postgres
Views: 179 · Posted: 2024-03-15

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh