作者 | JiekeXu
来源 |公众号 JiekeXu DBA之路(ID: JiekeXu_IT) 大家好,我是 JiekeXu,很高兴又和大家见面了,今天和大家一起来看看六脉神剑之使用 yum 安装 PostgreSQL,欢迎点击上方蓝字“JiekeXu DBA之路”关注我的公众号,标星或置顶,更多干货第一时间到达!
前 言
PostgreSQL 是一个基于 POSTGRES 的对象关系数据库管理系统(ORDBMS),版本4.2,由加州大学伯克利分校计算机科学系开发。PostgreSQL 最初设想于 1986 年,1986 年也被视为 PostgreSQL 发展史的元年,当时被叫做 Berkley Postgres Project。该项目一直到 1994 年都处于演进和修改中,直到开发人员 Andrew Yu 和 Jolly Chen 在 Postgres 中添加了一个 SQL(StructuredQuery Language,结构化查询语言)翻译程序,该版本叫做Postgres95,在开放源代码社区发放。1996 年,再次对 Postgres95 做了较大的改动,并将其作为 PostgresSQL 6.0 版发布。
2008 年发布了 PostgreSQL8.0,从这个版本开始支持 Windows 操作系统。 2010 年 9 月份,PostgreSQL 发行了 9.0.0 版本。 2017 年 10 月份发布了 10 版本。 2018 年 10 月份发布了 11 版本。 2019 年 9 月份发布了 12 版本。 2020 年 9 月份发布了 13 版本。 2021 年 9 月份发布了 14 版本。 2022 年 10 月份发布了 15 版本。 2023 年 9 月份发布了 16 版本。
db-engines 排行榜上 PG 排名第四名且一直处于上升趋势,在国内也比较火,未来发展趋势不可估量,且很多国产数据库也是基于 PG 的二次开发,很多功能原理相似,学习了解 PG 势在必行,下面我们来使用 yum 安装一个 PostgreSQL 数据库并简单的进行增删改查,方便快捷六步即可完成,特别适合开发测试和运维人员来初步学习和使用。另外生产环境一般使用编译安装,可以自定义编译一些参数,这里可以参考我以前写的CentOS6.7 安装 PostgreSQL10.9 详细教程。
系统要求:配置 1c2g 文件系统 10g, Linux 7 可上网即可安装 PG。
代码语言:javascript复制[root@jiekexu1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 46G 11G 36G 23% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 28K 1.9G 1% /dev/shm
tmpfs 1.9G 196M 1.7G 11% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 297M 157M 140M 53% /boot
tmpfs 378M 0 378M 0% /run/user/0
[root@jiekexu1 ~]#
[root@jiekexu1 ~]#
[root@jiekexu1 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@jiekexu1 ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 762M 281M 962M 2.7G 1.3G
Swap: 0B 0B 0B
[root@jiekexu1 ~]# ping www.baidu.com
PING www.baidu.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=128 time=10.5 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=128 time=12.9 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=128 time=15.9 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=4 ttl=128 time=8.45 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=5 ttl=128 time=11.9 ms
^C
--- www.baidu.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 8103ms
rtt min/avg/max/mdev = 8.451/11.988/15.997/2.514 ms
[root@jiekexu1 ~]# uname -a
Linux jiekexu1 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
代码语言:javascript复制1. 创建 postgres 用户
代码语言:javascript复制groupadd -g 1006 postgres
useradd -u 1006 -g 1006 postgres
echo "postgres" | passwd --stdin postgres
--passwd postgres
--密码:postgres
# 关闭 selinux:
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
cat /etc/selinux/config
# 关闭防火墙:
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
2. 修改 yum 源
代码语言:javascript复制
代码语言:javascript复制yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
代码语言:javascript复制[root@jiekexu1 ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Loaded plugins: fastestmirror, langpacks
pgdg-redhat-repo-latest.noarch.rpm | 8.6 kB 00:00:00
Examining /var/tmp/yum-root-4EAy0I/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-32.noarch
Marking /var/tmp/yum-root-4EAy0I/pgdg-redhat-repo-latest.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-redhat-repo.noarch 0:42.0-32 will be installed
--> Finished Dependency Resolution
base/7/x86_64 | 3.6 kB 00:00:00
extras/7/x86_64 | 2.9 kB 00:00:00
extras/7/x86_64/primary_db | 250 kB 00:00:00
mysql-connectors-community/x86_64 | 2.6 kB 00:00:00
mysql-connectors-community/x86_64/primary_db | 100 kB 00:00:00
mysql-tools-community/x86_64 | 2.6 kB 00:00:00
mysql-tools-community/x86_64/primary_db | 94 kB 00:00:00
mysql57-community/x86_64 | 2.6 kB 00:00:00
mysql57-community/x86_64/primary_db | 349 kB 00:00:00
updates/7/x86_64 | 2.9 kB 00:00:00
updates/7/x86_64/primary_db | 23 MB 00:00:10
Dependencies Resolved
=================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================
Installing:
pgdg-redhat-repo noarch 42.0-32 /pgdg-redhat-repo-latest.noarch 13 k
Transaction Summary
=================================================================================================================================
Install 1 Package
Total size: 13 k
Installed size: 13 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : pgdg-redhat-repo-42.0-32.noarch 1/1
Verifying : pgdg-redhat-repo-42.0-32.noarch 1/1
Installed:
pgdg-redhat-repo.noarch 0:42.0-32
Complete!
3. 使用 yum 直接安装
代码语言:javascript复制yum install -y postgresql13-server
代码语言:javascript复制
当然这里你也可以直接 yum install -y postgresql14-server 安装今年 8 月份发布的 14.9 版本,但是 15 版本的不能直接安装,需要依赖 libzstd.so.1 软件包才可以。我这里安装的是今年 8 月份发布的 13.12 版本。
代码语言:javascript复制[root@jiekexu1 ~]# yum install postgresql15-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package postgresql15-server.x86_64 0:15.4-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql15-libs(x86-64) = 15.4-1PGDG.rhel7 for package: postgresql15-server-15.4-1PGDG.rhel7.x86_64
--> Processing Dependency: postgresql15(x86-64) = 15.4-1PGDG.rhel7 for package: postgresql15-server-15.4-1PGDG.rhel7.x86_64
--> Processing Dependency: libzstd.so.1()(64bit) for package: postgresql15-server-15.4-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package postgresql15.x86_64 0:15.4-1PGDG.rhel7 will be installed
--> Processing Dependency: libzstd >= 1.4.0 for package: postgresql15-15.4-1PGDG.rhel7.x86_64
--> Processing Dependency: libzstd.so.1()(64bit) for package: postgresql15-15.4-1PGDG.rhel7.x86_64
---> Package postgresql15-libs.x86_64 0:15.4-1PGDG.rhel7 will be installed
---> Package postgresql15-server.x86_64 0:15.4-1PGDG.rhel7 will be installed
--> Processing Dependency: libzstd.so.1()(64bit) for package: postgresql15-server-15.4-1PGDG.rhel7.x86_64
--> Finished Dependency Resolution
Error: Package: postgresql15-server-15.4-1PGDG.rhel7.x86_64 (pgdg15)
Requires: libzstd.so.1()(64bit)
Error: Package: postgresql15-15.4-1PGDG.rhel7.x86_64 (pgdg15)
Requires: libzstd.so.1()(64bit)
Error: Package: postgresql15-15.4-1PGDG.rhel7.x86_64 (pgdg15)
Requires: libzstd >= 1.4.0
You could try using --skip-broken to work around the problem
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
直接安装则是找不到这个包,那么我们可以安装如下其他两个包代替,这样也就可以直接安装 15 版本的最新版了。
代码语言:javascript复制[root@jiekexu1 ~]# yum install libzstd*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
No package libzstd* available.
Error: Nothing to do
[root@jiekexu1 ~]# yum install epel-release.noarch -y
[root@jiekexu1 ~]# yum install libzstd.x86_64 -y
[root@jiekexu1 ~]# yum install postgresql15-server
下面我这里是直接安装 PG 13.12 版本。
代码语言:javascript复制[root@jiekexu1 ~]# yum install postgresql13-server -y
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.bfsu.edu.cn
pgdg-common/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg-common/7/x86_64/signature | 2.9 kB 00:00:00 !!!
pgdg11/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg11/7/x86_64/signature | 3.6 kB 00:00:00 !!!
pgdg12/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg12/7/x86_64/signature | 3.6 kB 00:00:00 !!!
pgdg13/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg13/7/x86_64/signature | 3.6 kB 00:00:00 !!!
pgdg14/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg14/7/x86_64/signature | 3.6 kB 00:00:00 !!!
pgdg15/7/x86_64/signature | 198 B 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg15/7/x86_64/signature | 3.6 kB 00:00:00 !!!
(1/11): pgdg11/7/x86_64/group_gz | 245 B 00:00:01
(2/11): pgdg13/7/x86_64/group_gz | 246 B 00:00:00
(3/11): pgdg-common/7/x86_64/primary_db | 200 kB 00:00:02
(4/11): pgdg12/7/x86_64/group_gz | 245 B 00:00:02
(5/11): pgdg14/7/x86_64/group_gz | 244 B 00:00:00
(6/11): pgdg15/7/x86_64/group_gz | 246 B 00:00:00
(7/11): pgdg15/7/x86_64/primary_db | 143 kB 00:00:00
(8/11): pgdg12/7/x86_64/primary_db | 405 kB 00:00:07
(9/11): pgdg13/7/x86_64/primary_db | 326 kB 00:00:15
(10/11): pgdg14/7/x86_64/primary_db | 225 kB 00:00:23
(11/11): pgdg11/7/x86_64/primary_db | 517 kB 00:00:38
Resolving Dependencies
--> Running transaction check
---> Package postgresql13-server.x86_64 0:13.12-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql13-libs(x86-64) = 13.12-1PGDG.rhel7 for package: postgresql13-server-13.12-1PGDG.rhel7.x86_64
--> Processing Dependency: postgresql13(x86-64) = 13.12-1PGDG.rhel7 for package: postgresql13-server-13.12-1PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql13-server-13.12-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package postgresql13.x86_64 0:13.12-1PGDG.rhel7 will be installed
---> Package postgresql13-libs.x86_64 0:13.12-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================
Installing:
postgresql13-server x86_64 13.12-1PGDG.rhel7 pgdg13 5.4 M
Installing for dependencies:
postgresql13 x86_64 13.12-1PGDG.rhel7 pgdg13 1.4 M
postgresql13-libs x86_64 13.12-1PGDG.rhel7 pgdg13 385 k
Transaction Summary
=================================================================================================================================
Install 1 Package ( 2 Dependent packages)
Total download size: 7.1 M
Installed size: 31 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/pgdg13/packages/postgresql13-libs-13.12-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Public key for postgresql13-libs-13.12-1PGDG.rhel7.x86_64.rpm is not installed
(1/3): postgresql13-libs-13.12-1PGDG.rhel7.x86_64.rpm | 385 kB 00:00:02
(2/3): postgresql13-13.12-1PGDG.rhel7.x86_64.rpm | 1.4 MB 00:00:03
(3/3): postgresql13-server-13.12-1PGDG.rhel7.x86_64.rpm | 5.4 MB 00:00:05
---------------------------------------------------------------------------------------------------------------------------------
Total 840 kB/s | 7.1 MB 00:00:08
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
Userid : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Package : pgdg-redhat-repo-42.0-32.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : postgresql13-libs-13.12-1PGDG.rhel7.x86_64 1/3
Installing : postgresql13-13.12-1PGDG.rhel7.x86_64 2/3
Installing : postgresql13-server-13.12-1PGDG.rhel7.x86_64 3/3
Verifying : postgresql13-13.12-1PGDG.rhel7.x86_64 1/3
Verifying : postgresql13-server-13.12-1PGDG.rhel7.x86_64 2/3
Verifying : postgresql13-libs-13.12-1PGDG.rhel7.x86_64 3/3
Installed:
postgresql13-server.x86_64 0:13.12-1PGDG.rhel7
Dependency Installed:
postgresql13.x86_64 0:13.12-1PGDG.rhel7 postgresql13-libs.x86_64 0:13.12-1PGDG.rhel7
Complete!
4. 初始化数据库
代码语言:javascript复制postgresql-13-setup initdb
代码语言:javascript复制
代码语言:javascript复制[root@jiekexu1 ~]# postgresql-13-setup initdb
Initializing database ... OK
代码语言:javascript复制
初始化之后便会生成 postgresql.conf 配置文件,我们查一下默认的路径为 /var/lib/pgsql 目录。
代码语言:javascript复制[root@jiekexu1 log]# find / -name postgresql.conf
/var/lib/pgsql/13/data/postgresql.conf
/opt/opengauss3.1/data/db/postgresql.conf
[root@jiekexu1 log]# more /var/lib/pgsql/13/data/postgresql.conf
[root@jiekexu1 log]# cd /var/lib/pgsql/13
[root@jiekexu1 13]# ll
total 8
drwx------ 2 postgres postgres 6 Aug 9 21:02 backups
drwx------ 20 postgres postgres 4096 Oct 10 00:45 data
-rw------- 1 postgres postgres 921 Oct 10 00:27 initdb.log
[root@jiekexu1 13]# more initdb.log
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 /var/lib/pgsql/13/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:
/usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data/ -l logfile start
[root@jiekexu1 13]#
5. 使用 system 启动数据库
代码语言:javascript复制# 默认初始化数据库路径:
/var/lib/pgsql/13/data
# 启动数据库
systemctl enable postgresql-13
systemctl start postgresql-13
代码语言:javascript复制
代码语言:javascript复制[root@jiekexu1 ~]# systemctl enable postgresql-13
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-13.service to /usr/lib/systemd/system/postgresql-13.service.
[root@jiekexu1 ~]# systemctl start postgresql-13
Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.
[root@jiekexu1 ~]# systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2023-10-10 00:27:59 CST; 43s ago
Docs: https://www.postgresql.org/docs/13/static/
Process: 79519 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
Process: 79512 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 79519 (code=exited, status=1/FAILURE)
Oct 10 00:27:58 jiekexu1 systemd[1]: Starting PostgreSQL 13 database server...
Oct 10 00:27:59 jiekexu1 postmaster[79519]: 2023-10-10 00:27:59.239 CST [79519] LOG: redirecting log output to logging ...rocess
Oct 10 00:27:59 jiekexu1 postmaster[79519]: 2023-10-10 00:27:59.239 CST [79519] HINT: Future log output will appear in ..."log".
Oct 10 00:27:59 jiekexu1 systemd[1]: postgresql-13.service: main process exited, code=exited, status=1/FAILURE
Oct 10 00:27:59 jiekexu1 systemd[1]: Failed to start PostgreSQL 13 database server.
Oct 10 00:27:59 jiekexu1 systemd[1]: Unit postgresql-13.service entered failed state.
Oct 10 00:27:59 jiekexu1 systemd[1]: postgresql-13.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@jiekexu1 ~]# journalctl -xe
--
-- Unit session-36653.scope has finished starting up.
--
-- The start-up result is done.
Oct 10 00:28:01 jiekexu1 CROND[79527]: (omm) CMD (source ~/.bashrc;python3 /opt/opengauss3.1/install/om/script/local/CheckSshAgen
Oct 10 00:28:01 jiekexu1 systemd[1]: Removed slice User Slice of omm.
-- Subject: Unit user-1002.slice has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit user-1002.slice has finished shutting down.
Oct 10 00:29:01 jiekexu1 systemd[1]: Created slice User Slice of omm.
-- Subject: Unit user-1002.slice has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit user-1002.slice has finished starting up.
--
-- The start-up result is done.
Oct 10 00:29:01 jiekexu1 systemd[1]: Started Session 36654 of user omm.
-- Subject: Unit session-36654.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-36654.scope has finished starting up.
--
-- The start-up result is done.
Oct 10 00:29:01 jiekexu1 CROND[79660]: (omm) CMD (source ~/.bashrc;python3 /opt/opengauss3.1/install/om/script/local/CheckSshAgen
Oct 10 00:29:01 jiekexu1 systemd[1]: Removed slice User Slice of omm.
-- Subject: Unit user-1002.slice has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit user-1002.slice has finished shutting down.
很遗憾报错了,根据上面提示使用 -l 显示更多信息。HINT: Future log output will appear in directory "log"
代码语言:javascript复制[root@jiekexu1 13]# systemctl status postgresql-13.service -l
● postgresql-13.service - PostgreSQL 13 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2023-10-10 00:37:02 CST; 13min ago
Docs: https://www.postgresql.org/docs/13/static/
Process: 80832 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
Process: 80826 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 80832 (code=exited, status=1/FAILURE)
Oct 10 00:37:02 jiekexu1 systemd[1]: Starting PostgreSQL 13 database server...
Oct 10 00:37:02 jiekexu1 postmaster[80832]: 2023-10-10 00:37:02.865 CST [80832] LOG: redirecting log output to logging collector process
Oct 10 00:37:02 jiekexu1 postmaster[80832]: 2023-10-10 00:37:02.865 CST [80832] HINT: Future log output will appear in directory "log".
Oct 10 00:37:02 jiekexu1 systemd[1]: postgresql-13.service: main process exited, code=exited, status=1/FAILURE
Oct 10 00:37:02 jiekexu1 systemd[1]: Failed to start PostgreSQL 13 database server.
Oct 10 00:37:02 jiekexu1 systemd[1]: Unit postgresql-13.service entered failed state.
Oct 10 00:37:02 jiekexu1 systemd[1]: postgresql-13.service failed.
由此我们知道错误日志在 log 文件夹里面。/var/lib/pgsql/13/data/log/postgresql-Tue.log
[root@jiekexu1 log]# cd /var/lib/pgsql/13/data/log
[root@jiekexu1 log]# pwd
/var/lib/pgsql/13/data/log
[root@jiekexu1 log]# ll
total 4
-rw------- 1 postgres postgres 3333 Oct 10 00:46 postgresql-Tue.log
[root@jiekexu1 log]# more postgresql-Tue.log
2023-10-10 00:27:59.239 CST [79519] LOG: starting PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623
(Red Hat 4.8.5-44), 64-bit
2023-10-10 00:27:59.240 CST [79519] LOG: could not bind IPv6 address "::1": Address already in use
2023-10-10 00:27:59.240 CST [79519] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:27:59.240 CST [79519] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:27:59.240 CST [79519] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:27:59.240 CST [79519] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:27:59.240 CST [79519] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:27:59.240 CST [79519] WARNING: could not create listen socket for "localhost"
2023-10-10 00:27:59.240 CST [79519] FATAL: could not create any TCP/IP sockets
2023-10-10 00:27:59.242 CST [79519] LOG: database system is shut down
2023-10-10 00:37:02.865 CST [80832] LOG: starting PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623
(Red Hat 4.8.5-44), 64-bit
2023-10-10 00:37:02.869 CST [80832] LOG: could not bind IPv6 address "::1": Address already in use
2023-10-10 00:37:02.869 CST [80832] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:37:02.869 CST [80832] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:37:02.869 CST [80832] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:37:02.869 CST [80832] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:37:02.869 CST [80832] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:37:02.869 CST [80832] WARNING: could not create listen socket for "localhost"
2023-10-10 00:37:02.869 CST [80832] FATAL: could not create any TCP/IP sockets
2023-10-10 00:37:02.871 CST [80832] LOG: database system is shut down
2023-10-10 00:46:18.607 CST [82413] LOG: starting PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623
(Red Hat 4.8.5-44), 64-bit
2023-10-10 00:46:18.609 CST [82413] LOG: could not bind IPv6 address "::1": Address already in use
2023-10-10 00:46:18.609 CST [82413] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:46:18.609 CST [82413] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:46:18.609 CST [82413] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:46:18.609 CST [82413] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 00:46:18.609 CST [82413] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and ret
ry.
2023-10-10 00:46:18.609 CST [82413] WARNING: could not create listen socket for "localhost"
2023-10-10 00:46:18.609 CST [82413] FATAL: could not create any TCP/IP sockets
2023-10-10 00:46:18.610 CST [82413] LOG: database system is shut down
通过日志看到无法启动的原因则是 5432 端口被占用 “could not create any TCP/IP sockets”,原先安装过 opengauss3.1 版本,占用了 5432 端口,那么我们将端口修改为 54321 吧。
代码语言:javascript复制[root@jiekexu1 bin]# netstat -anlp | grep 5432
tcp 0 0 192.168.75.11:5432 0.0.0.0:* LISTEN 24405/gaussdb
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 24405/gaussdb
tcp6 0 0 ::1:5432 :::* LISTEN 24405/gaussdb
unix 2 [ ACC ] STREAM LISTENING 5833403 24405/gaussdb /opt/opengauss3.1/tmp/.s.PGSQL.5432
[root@jiekexu1 ~]# vim /var/lib/pgsql/13/data/postgresql.conf
[root@jiekexu1 ~]# more /var/lib/pgsql/13/data/postgresql.conf | grep -w 'port'
port = 54321 # (change requires restart)
# %r = remote host and port
[root@jiekexu1 ~]# systemctl start postgresql-13
正常启动 postgresql-Tue.log 日志如下
代码语言:javascript复制
代码语言:javascript复制2023-10-10 01:15:23.995 CST [87696] LOG: starting PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-10-10 01:15:23.995 CST [87696] LOG: listening on IPv6 address "::1", port 54321
2023-10-10 01:15:23.995 CST [87696] LOG: listening on IPv4 address "127.0.0.1", port 54321
2023-10-10 01:15:23.995 CST [87696] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2023-10-10 01:15:23.995 CST [87696] HINT: Is another postmaster already running on port 54321? If not, wait a few seconds and retry.
2023-10-10 01:15:23.997 CST [87696] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.54321"
2023-10-10 01:15:24.000 CST [87696] LOG: listening on Unix socket "/tmp/.s.PGSQL.54321"
2023-10-10 01:15:24.002 CST [87699] LOG: database system was shut down at 2023-10-10 00:27:11 CST
2023-10-10 01:15:24.045 CST [87696] LOG: database system is ready to accept connections
代码语言:javascript复制6. 登录数据库
代码语言:javascript复制su - postgres
psql -U postgres -d postgres -p 54321
代码语言:javascript复制
查看版本,如果是默认端口,直接使用 psql -version 则可以看到版本为 13.12,否则还需要如下添加端口号才可以。
代码语言:javascript复制[root@jiekexu1 ~]# su - postgres
Last login: Tue Oct 10 00:46:15 CST 2023 on pts/0
[postgres@jiekexu1 ~]$ psql -version
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
[postgres@jiekexu1 ~]$ psql -version -p 54321
psql (13.12)
Type "help" for help.
postgres=# q
代码语言:javascript复制
登录数据库
代码语言:javascript复制[postgres@jiekexu1 ~]$ psql -U postgres -d postgres -p 54321
psql (13.12)
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=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)
使用数据库
创建数据库,查看数据库,删除数据库
代码语言:javascript复制postgres=# select * from pg_database;
postgres=# create database testdb;
postgres=# create database JiekeXu;
CREATE DATABASE
postgres=# l
List ofdatabases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------- ---------- ---------- --------- ------- -----------------------
jiekexu | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres
| | | | | postgres=CTc/postgres
testdb | postgres | UTF8 | C | C |
(5 rows)
postgres=# drop database TestDB;
DROP DATABASE
postgres=# l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------- ---------- ---------- --------- ------- -----------------------
jiekexu | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres
| | | | | postgres=CTc/postgres
(4 rows)
创建表,查看表,删除表,截断表
1、查看数据库大小
代码语言:javascript复制postgres=# select pg_database.datname, pg_size_pretty (pg_database_size(pg_database.datname)) AS size from pg_database;
datname | size
----------- ---------
postgres | 7885 kB
jiekexu | 7901 kB
template1 | 7737 kB
template0 | 7737 kB
testdb | 7737 kB
(5 rows)
代码语言:javascript复制
2、查看某一个数据库大小
代码语言:javascript复制postgres=# select pg_database_size('jiekexu');
pg_database_size
------------------
8090159
(1 row)
代码语言:javascript复制
3、按顺序查看索引
代码语言:javascript复制
代码语言:javascript复制select indexrelname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_indexes where schemaname='public' order by pg_relation_size(relid) desc;
代码语言:javascript复制4、查看所有表的大小
代码语言:javascript复制
代码语言:javascript复制select relname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables where schemaname='public' order by pg_relation_size(relid) desc;
代码语言:javascript复制切换数据库
代码语言:javascript复制postgres=# c jiekexu
You are now connected to database"jiekexu" as user “postgres”.
创建表
代码语言:javascript复制jiekexu=# create table test(id int,name char(20), age int);
CREATE TABLE
jiekexu=#
jiekexu=# insert into test values(1,'JiekeXu',18);
INSERT 0 1
jiekexu=# insert into test values(2,'Adventure',25);
INSERT 0 1
jiekexu=#
代码语言:javascript复制
查看表信息
代码语言:javascript复制jiekexu=# d
List of relations
Schema | Name | Type | Owner
-------- ------- ------- ----------
public | test | table | postgres
public | test1 | table | postgres
public | test2 | table | postgres
(3 rows)
jiekexu=# d test
Table"public.test"
Column | Type | Collation | Nullable |Default
-------- --------------- ----------- ---------- ---------
id |integer | | |
name |character(20) | | |
age |integer
代码语言:javascript复制
查看表内容
代码语言:javascript复制jiekexu=# select * from test;
id| name | age
---- ---------------------- -----
1 |JiekeXu | 18
2 |Adventure | 25
(2 rows)
代码语言:javascript复制
修改表内容
代码语言:javascript复制jiekexu=# update test set age=26 where name='JiekeXu';
UPDATE 1
jiekexu=# commit;
WARNING: there is no transaction in progress
COMMIT
jiekexu=# select * from test;
id| name | age
---- ---------------------- -----
2 |Adventure | 25
1 |JiekeXu | 26
(2 rows)
代码语言:javascript复制
删除表
代码语言:javascript复制drop table test2;
delete from test;
jiekexu=# select * from test;
id| name | age
---- ---------------------- -----
2 |Adventure | 25
1 |JiekeXu | 26
2 |BreatHeat | 22
(3 rows)
jiekexu=# delete from test where age=22;
DELETE 1
jiekexu=# select * from test;
id| name | age
---- ---------------------- -----
2 |Adventure | 25
1 |JiekeXu | 26
(2 rows)
jiekexu=# drop table test2;
DROP TABLE
jiekexu=#
jiekexu=# d
List of relations
Schema | Name | Type | Owner
-------- ------- ------- ----------
public | test | table | postgres
public | test1 | table | postgres
(2 rows)
代码语言:javascript复制
截断表
Truncate 截断表如果不是自动提交则是可以回滚,则 truncate 属于 DML 语句。
代码语言:javascript复制jiekexu=# create table t (id int);
CREATE TABLE
jiekexu=# insert into t values (1),(2),(3),(4);
INSERT 0 4
jiekexu=# d
List of relations
Schema | Name | Type | Owner
-------- ------ ------- ----------
public | t | table | postgres
(1 row)
jiekexu=# select * from t;
id
----
1
2
3
4
(4 rows)
jiekexu=# commit;
WARNING: there is no transaction in progress
COMMIT
jiekexu=#
jiekexu=# begin;
BEGIN
jiekexu=*# truncate table t;
TRUNCATE TABLE
jiekexu=*# select * from t;
id
----
(0 rows)
jiekexu=*# rollback;
ROLLBACK
jiekexu=# select * from t;
id
----
1
2
3
4
(4 rows)
代码语言:javascript复制
查看用户
代码语言:javascript复制postgres=# select * from pg_user;
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
---------- ---------- ------------- ---------- --------- -------------- ---------- ---------- -----------
postgres | 10 | t | t | t | t | ******** | |
(1 row)
代码语言:javascript复制
查看数据库 jiekexu 用户的拥有者是 postgres
代码语言:javascript复制postgres=# l jiekexu
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------- ---------- ---------- --------- ------- -------------------
jiekexu | postgres | UTF8 | C | C |
(1 row)
代码语言:javascript复制
最后,使用 q 或者 Ctrl d 退出命令行
代码语言:javascript复制postgres=# q
[postgres@jiekexu1 ~]$
代码语言:javascript复制
PG 常用元命令
全文完,希望可以帮到正在阅读的你,如果觉得此文对你有帮助,可以分享给你身边的朋友,同事,你关心谁就分享给谁,一起学习共同进步~~~