## 初始化数据库并设置自动启动 [root@Rocky8-1 ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb Initializing database ... OK
[root@Rocky8-1 ~]# systemctl enable postgresql-15 Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-15.service → /usr/lib/systemd/system/postgresql-15.service.
[root@Rocky8-1 ~]# systemctl start postgresql-15
## 验证 [root@Rocky8-1 ~]# sudo -u postgres psql -c "select version();" could not change directory to "/root": Permission denied version --------------------------------------------------------------------------------------------------------- PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-18), 64-bit (1 row)
## 切换postgres用户,进入数据库 [root@Rocky8-1 ~]# su - postgres [postgres@Rocky8-1 ~]$ psql psql (15.3) Type "help"forhelp.
postgres=# help You are using psql, the command-line interface to PostgreSQL. Type: \copyright for distribution terms \h forhelp with SQL commands \? forhelp with psql commands \g or terminate with semicolon to execute query \q to quit postgres=#
[root@Rocky8-2 ~]# yum install -y gcc make readline-devel zlib-devel
下载解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@Rocky8-2 ~]# wget https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz [root@Rocky8-2 ~]# tar xf postgresql-15.3.tar.gz
[root@Rocky8-2 ~]# cd postgresql-15.3/ [root@Rocky8-2 postgresql-15.3]# ll total 780 -rw-r--r-- 1 1107 1107 397 May 9 05:13 aclocal.m4 drwxrwxrwx 2 1107 1107 4096 May 9 05:25 config -rwxr-xr-x 1 1107 1107 601977 May 9 05:13 configure -rw-r--r-- 1 1107 1107 89369 May 9 05:13 configure.ac drwxrwxrwx 61 1107 1107 4096 May 9 05:24 contrib -rw-r--r-- 1 1107 1107 1192 May 9 05:13 COPYRIGHT drwxrwxrwx 3 1107 1107 87 May 9 05:25 doc -rw-r--r-- 1 1107 1107 4264 May 9 05:13 GNUmakefile.in -rw-r--r-- 1 1107 1107 277 May 9 05:13 HISTORY -rw-r--r-- 1 1107 1107 63842 May 9 05:26 INSTALL -rw-r--r-- 1 1107 1107 1875 May 9 05:13 Makefile -rw-r--r-- 1 1107 1107 1213 May 9 05:13 README drwxrwxrwx 16 1107 1107 4096 May 9 05:26 src
查看官方安装说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@Rocky8-2 postgresql-15.3]# cat INSTALL | less .... Short Version
./configure make su make install adduser postgres mkdir -p /usr/local/pgsql/data chown postgres /usr/local/pgsql/data su - postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start /usr/local/pgsql/bin/createdb test /usr/local/pgsql/bin/psql test
The long version is the rest of this document. ....
## 生产环境建议初始化方式 [root@Rocky8-2 postgresql-15.3]# su - postgres [postgres@Rocky8-2 ~]$ initdb -A md5 -D $PGDATA -E utf8 --locale=C -U postgres -W 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 "C". The default text search configuration will be set to "english".
Data page checksums are disabled.
Enter new superuser password: Enter it again:
fixing permissions on existing directory /pgsql/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
Success. You can now start the database server using:
## 启动数据库 [postgres@Rocky8-2 ~]$ pg_ctl -D /pgsql/data -l /pgsql/log/logfile start waiting for server to start.... done server started ## 关闭数据库 1、smart 关闭:等所有连接中止后,关闭数据库。如果客户端连接不终止,则无法关闭数据库 [postgres@Rocky8-2 ~]$ pg_ctl stop -D /pgsql/data/ -ms
2、fast 关闭:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。此项为默认值,建议使用 [postgres@Rocky8-2 ~]$ pg_ctl stop -D /pgsql/data/ -mf waiting for server to shut down.... done server stopped
3、immediate: 相当于 kill -9,立即关闭数据库,数据库进程立即停止,直接退出,下次启动数据库需要进行恢复。 [postgres@Rocky8-2 ~]$ pg_ctl stop -D /pgsql/data/ -mi ## 重启数据库 [postgres@Rocky8-2 ~]$ pg_ctl restart -l /pgsql/log/logfile -mf pg_ctl: PID file "/pgsql/data/postmaster.pid" does not exist Is server running? trying to start server anyway waiting for server to start.... done server started ## 修改配置后重新加载配置 [postgres@Rocky8-2 ~]$ pg_ctl reload -D /pgsql/data