本手册基于 Windows 下的 wsl (CentOS Linux release 7.9.2009 (Core)) 操作系统安装,其他环境请酌情参考
数据库:https://www.postgresql.org
资源地址:https://www.postgresql.org/ftp/source/
使用的版本:https://www.postgresql.org/ftp/source/v10.19/
下载链接:https://ftp.postgresql.org/pub/source/v10.19/postgresql-10.19.tar.gz
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
tar -xzvf postgresql-10.19.tar.gz
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# mkdir postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ll
total 24636
drwxr-xr-x 2 root root 4096 Dec 11 17:54 postgresql
drwxrwxrwx 6 1107 1107 4096 Nov 9 2021 postgresql-10.19
-rw-r--r-- 1 root root 25216304 Dec 11 17:46 postgresql-10.19.tar.gz
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]#
# 获取编译路径
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# cd ./postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# pwd
/home/data/soft/postgresql/postgresql
# 开始编译
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# cd ../postgresql-10.19
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# ls
aclocal.m4 config configure configure.in contrib COPYRIGHT doc GNUmakefile.in HISTORY INSTALL Makefile README src
# 配置文件生成路径
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# ./configure --prefix=/home/data/soft/postgresql/postgresql
# 一大堆,不粘了,没报错就行
选项 | 描述 |
---|---|
–prefix=prefix | 安装到prefix指向的目录;默认为/usr/local/pgsql |
–bindir=dir | 安装应用程序到dir;默认为prefix/bin |
–with-docdir=dir | 安装文档到dir;默认为prefix/doc |
–with-pgport=port | 设置默认的服务器端网络连接服务TCP端口号 |
–with-tcl | 为服务端提供Tcl存储过程支持 |
–with-perl | 为服务端提供Perl存储过程支持 |
–with-python | 为服务端提供Python存储过程支持 |
# 这俩命令执行过程也都一大堆,不粘了,不报错就没事
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# make
# 最后是这样的
make -C config all
make[1]: Entering directory `/home/data/soft/postgresql/postgresql-10.19/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/data/soft/postgresql/postgresql-10.19/config'
All of PostgreSQL successfully made. Ready to install.
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# make install
# 最后是这样的
make -C config install
make[1]: Entering directory `/home/data/soft/postgresql/postgresql-10.19/config'
/usr/bin/mkdir -p '/home/data/soft/postgresql/postgresql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/home/data/soft/postgresql/postgresql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/home/data/soft/postgresql/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/home/data/soft/postgresql/postgresql-10.19/config'
PostgreSQL installation complete.
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# pwd
/home/data/soft/postgresql/postgresql-10.19
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql-10.19]# cd ../postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ll
total 16
drwxr-xr-x 2 root root 4096 Dec 11 18:09 bin
drwxr-xr-x 6 root root 4096 Dec 11 18:09 include
drwxr-xr-x 4 root root 4096 Dec 11 18:09 lib
drwxr-xr-x 6 root root 4096 Dec 11 18:09 share
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# groupadd postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# useradd -g postgres postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# id postgres
uid=1001(postgres) gid=1002(postgres) groups=1002(postgres)
创建数据目录,赋予新用户权限
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# pwd
/home/data/soft/postgresql/postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# mkdir data
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# chown postgres:postgres data
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# ls -al
total 28
drwxr-xr-x 7 root root 4096 Dec 11 18:14 .
drwxr-xr-x 4 root root 4096 Dec 11 17:54 ..
drwxr-xr-x 2 root root 4096 Dec 11 18:09 bin
drwxr-xr-x 2 postgres postgres 4096 Dec 11 18:14 data
drwxr-xr-x 6 root root 4096 Dec 11 18:09 include
drwxr-xr-x 4 root root 4096 Dec 11 18:09 lib
drwxr-xr-x 6 root root 4096 Dec 11 18:09 share
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgresql]# cd /home/postgres
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# ls -al
total 20
drwx------ 2 postgres postgres 4096 Dec 11 18:12 .
drwxr-xr-x. 5 root root 4096 Dec 11 18:12 ..
-rw-r--r-- 1 postgres postgres 18 Nov 25 2021 .bash_logout
-rw-r--r-- 1 postgres postgres 193 Nov 25 2021 .bash_profile
-rw-r--r-- 1 postgres postgres 231 Nov 25 2021 .bashrc
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# vim .bash_profile
# 修改后的文件内容如下
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PGHOME=/home/data/soft/postgresql/postgresql
export PGDATA=/home/data/soft/postgresql/postgresql/data/
# 遇到一个 Error while loading shared libraries: libpq.so.5 的错误,配置一下 lib 路径就好了
export LD_LIBRARY_PATH=/home/data/soft/postgresql/postgresql/lib
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH
[root@iZ0jlaqsl7s3tfa0rtwq5hZ postgres]# source .bash_profile
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ initdb
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/data/soft/postgresql/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
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:
pg_ctl -D /home/data/soft/postgresql/postgresql/data/ -l logfile start
# 查看数据文件
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ cd /home/data/soft/postgresql/postgresql/data/
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ ll
total 112
drwx------ 5 postgres postgres 4096 Dec 11 18:51 base
drwx------ 2 postgres postgres 4096 Dec 11 18:51 global
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_commit_ts
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_dynshmem
-rw------- 1 postgres postgres 4513 Dec 11 18:50 pg_hba.conf
-rw------- 1 postgres postgres 1636 Dec 11 18:50 pg_ident.conf
drwx------ 4 postgres postgres 4096 Dec 11 18:51 pg_logical
drwx------ 4 postgres postgres 4096 Dec 11 18:50 pg_multixact
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_notify
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_replslot
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_serial
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_snapshots
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_stat
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_stat_tmp
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_subtrans
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_tblspc
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_twophase
-rw------- 1 postgres postgres 3 Dec 11 18:50 PG_VERSION
drwx------ 3 postgres postgres 4096 Dec 11 18:50 pg_wal
drwx------ 2 postgres postgres 4096 Dec 11 18:50 pg_xact
-rw------- 1 postgres postgres 88 Dec 11 18:50 postgresql.auto.conf
-rw------- 1 postgres postgres 23001 Dec 11 18:50 postgresql.conf
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ vim postgresql.conf
## 修改内容如下:
listen_addresses = '*' # what IP address(es) to listen on;
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ data]$ vim pg_hba.conf
## 修改内容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
[root@iZ0jlaqsl7s3tfa0rtwq5hZ bin]# cd /home/data/soft/postgresql/postgresql-10.19/contrib/start-scripts/
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# ls
freebsd linux macos osx
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chmod a+x linux
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# cp linux /etc/init.d/postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# vim /etc/init.d/postgresql
# 修改内容如下
# Installation prefix 数据库文件安装位置
prefix=/home/data/soft/postgresql/postgresql
# Data directory 数据库中数据存放位置
PGDATA="/home/data/soft/postgresql/postgresql/data"
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chkconfig --add postgresql
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# chkconfig
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
redis_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]# service postgresql start
Starting PostgreSQL: ok
[root@iZ0jlaqsl7s3tfa0rtwq5hZ start-scripts]#
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ psql
psql (10.19)
Type "help" for help.
# 查看当前数据库
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
# 创建新数据库
postgres=# CREATE DATABASE NAMIS;
CREATE DATABASE
# 创建用户并配置密码
postgres=# CREATE USER namis WITH PASSWORD 'Hywd@1qaz';
CREATE ROLE
# 赋予用户角色
postgres=# ALTER ROLE namis SUPERUSER;
ALTER ROLE
postgres=# \q
# 使用新用户登录
[postgres@iZ0jlaqsl7s3tfa0rtwq5hZ ~]$ psql namis -U namis
psql (10.19)
Type "help" for help.
namis=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
namis | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
namis=# \q
我这里使用的是本地 wsl,默认端口和主机相同,所以不需要开放端口,其他环境请酌情处理
PostgreSQL 数据的data 文件夹不允许全组都有所有权限,我这里就是直接放权导致数据库无法启动
现象如下
# 首先使用 service postgresql start 命令启动,显示启动成功但是无法链接,也查不到数据库进程
# 后使用命令行的方式手动启动,报错如下
[postgres@LAPTOP-19HO0GA3 ~]$ pg_ctl start -D /home/soft/postgresql/postgresql/data/
waiting for server to start....2022-12-12 13:53:56.754 CST [320] FATAL: data directory "/home/soft/postgresql/postgresql/data" has group or world access
2022-12-12 13:53:56.754 CST [320] DETAIL: Permissions should be u=rwx (0700).
stopped waiting
pg_ctl: could not start server
Examine the log output.
解决方案
# 重新为data 文件夹赋权
chmod -R 0700 data