N86第七周作业

一、pg和mysql的优劣势。

总结pg和mysql的优劣势

特性MySQLPostgreSQL
实例通过执行MySQL命令(mysqld)启动实例。一个实例可以管理一个或多个数据库。一台服务器可以运行多个mysqld实例。一个实例管理器可以监视mysqld的各个实例。通过执行Postmaster进程(pg_ctl)启动实例。一个实例可以管理一个或多个数据库,这些数据库组成一个集群。集群是磁盘上的一个区域,这个区域在安装时初始化并由一个目录组成,所有数据都存储在这个目录中。使用initdb创建第一个数据库。一台机器上可以启动多个实例。
数据库数据库是命名的对象集合,是与实例中的其他数据库分离的实体。一个MySQL实例中的所有数据库共享同一个系统编目。数据库是命名的对象集合,每个数据库是与其他数据库分离的实体。每个数据库有自己的系统编目,但是所有数据库共享pg_databases。
数据缓冲区通过innodb_buffer_pool_size配置参数设置数据缓冲区。这个参数是内存缓冲区的字节数,InnoDB使用这个缓冲区来缓存表的数据和索引。在专用的数据库服务器上,这个参数最高可以设置为机器物理内存量的80%。Shared_buffers缓存。在默认情况下分配64个缓冲区。默认的块大小是8K。可以通过设置postgresql.conf文件中的shared_buffers参数来更新缓冲区缓存。
数据库连接客户机使用CONNECT或USE语句连接数据库,这时要指定数据库名,还可以指定用户id和密码。使用角色管理数据库中的用户和用户组。客户机使用connect语句连接数据库,这时要指定数据库名,还可以指定用户id和密码。使用角色管理数据库中的用户和用户组。
身份验证MySQL在数据库级管理身份验证。基本只支持密码认证。PostgreSQL支持丰富的认证方法:信任认证、口令认证、Kerberos认证、基于Ident的认证、LDAP认证、PAM认证
加密可以在表级指定密码来对数据进行加密。还可以使用AES_ENCRYPT和AES_DECRYPT函数对列数据进行加密和解密。可以通过SSL连接实现网络加密。可以使用pgcrypto库中的函数对列进行加密/解密。可以通过SSL连接实现网络加密。
审计可以对querylog执行grep。可以在表上使用 PL/pgSQL 触发器来进行审计。
查询解释使用 EXPLAIN 命令查看查询的解释 计划。使用 EXPLAIN 命令查看查询的解释计划。
备份、恢复和日志InnoDB使用写前(write-ahead)日志记录。支持在线和离线完全备份以及崩溃和事务恢复。需要第三方软件才能支持热备份。在数据目录的一个子目录中维护写前日志。支持在线和离线完全备份以及崩溃、时间点和事务恢复。可以支持热备份。
JDBC驱动程序可以从 参考资料 下载 JDBC 驱动程序。可以从 参考资料 下载 JDBC 驱动程序。
表类型取决于存储引擎。例如,NDB存储引擎支持分区表,内存引擎支持内存表。支持临时表、常规表以及范围和列表类型的分区表。不支持哈希分区表。由于PostgreSQL的表分区是通过表继承和规则系统完成了,所以可以实现更复杂的分区方式。
索引类型取决于存储引擎。MyISAM:BTREE,InnoDB:BTREE。支持 B-树、哈希、R-树和 Gist 索引。
约束支持主键、外键、惟一和非空约束。对检查约束进行解析,但是不强制实施。支持主键、外键、惟一、非空和检查约束。
存储过程和用户定义函数支持CREATEPROCEDURE和CREATEFUNCTION语句。存储过程可以用SQL和C++编写。用户定义函数可以用SQL、C和C++编写。没有单独的存储过程,都是通过函数实现的。用户定义函数可以用PL/pgSQL(专用的过程语言)、PL/Tcl、PL/Perl、PL/Python、SQL和C编写。
触发器支持行前触发器、行后触发器和语句触发器,触发器语句用过程语言复合语句编写。支持行前触发器、行后触发器和语句触发器,触发器过程用C编写。
系统配置文件my.confPostgresql.conf
数据库配置my.confPostgresql.conf
客户机连接文件my.confpg_hba.conf
XML支持有限的 XML 支持。有限的 XML 支持。
数据访问和管理服务器OPTIMIZETABLE——回收未使用的空间并消除数据文件的碎片myisamchk-analyze——更新查询优化器所使用的统计数据(MyISAM存储引擎)mysql——命令行工具MySQLAdministrator——客户机GUI工具Vacuum——回收未使用的空间Analyze——更新查询优化器所使用的统计数据psql——命令行工具pgAdmin——客户机GUI工具
并发控制支持表级和行级锁。InnoDB存储引擎支持READ_COMMITTED、READ_UNCOMMITTED、REPEATABLE_READ和SERIALIZABLE。使用SETTRANSACTIONISOLATIONLEVEL语句在事务级设置隔离级别。支持表级和行级锁。支持的ANSI隔离级别是ReadCommitted(默认——能看到查询启动时数据库的快照)和Serialization(与RepeatableRead相似——只能看到在事务启动之前提交的结果)。使用SETTRANSACTION语句在事务级设置隔离级别。使用SETSESSION在会话级进行设置。
引擎协议最重要的引擎InnoDB很早就由Oracle公司控制。目前整个MySQL数据库都由Oracle控制。BSD协议,没有被大公司垄断。
复杂查询的处理对复杂查询的处理较弱,查询优化器不够成熟很强大的查询优化器,支持很复杂的查询处理。
表连接类型只有一种表连接类型:嵌套循环连接(nested-loop),不支持排序-合并连接(sort-mergejoin)与散列连接(hashjoin)。都支持
性能优化信息性能优化工具与度量信息不足提供了一些性能视图,可以方便的看到发生在一个表和索引上的select、delete、update、insert统计信息,也可以看到cache命中率。网上有一个开源的pgstatspack工具。
主键要求InnoDB的表和索引都是按相同的方式存储。也就是说表都是索引组织表。这一般要求主键不能太长而且插入时的主键最好是按顺序递增,否则对性能有很大影响。不存在这个问题。
大部分查询只能使用表上的单一索引;在某些情况下,会存在使用多个索引的查询,但是查询优化器通常会低估其成本,它们常常比表扫描还要慢。不存在这个问题。
表增加列,基本上是重建表和索引,会花很长时间。表增加列,只是在数据字典中增加表定义,不会重建表
存储过程与触发器的功能有限。可用来编写存储过程、触发器、计划事件以及存储函数的语言功能较弱除支持pl/pgsql写存储过程,还支持perl、python、Tcl类型的存储过程:pl/perl,pl/python,pl/tcl。也支持用C语言写存储过程。
Sequence不支持Sequence。支持
函数索引不支持函数索引,只能在创建基于具体列的索引。不支持物化视图。支持函数索引,同时还支持部分数据索引,通过规则系统可以实现物化视图的功能。
执行计划是否共享执行计划并不是全局共享的,仅仅在连接内部是共享的。执行计划共享
SQL语法MySQL支持的SQL语法(ANSISQL标准)的很小一部分。不支持递归查询、通用表表达式(Oracle的with语句)或者窗口函数(分析函数)。都支持
自定义类型或域不支持用户自定义类型或域(domain)支持
秒级别的存储类型对于时间、日期、间隔等时间类型没有秒以下级别的存储类型可以精确到秒以
下。
身份验证身份验证功能是完全内置的,不支持操作系统认证、PAM认证,不支持LDAP以及其它类似的外部身份验证功能。支持OS认证、Kerberos认证、Ident的认证、LDAP认证、PAM认证
dblink不支持databaselink。有一种叫做Federated的存储引擎可以作为一个中转将查询语句传递到远程服务器的一个表上,不过,它功能很粗糙并且漏洞很多有dblink,同时还有一个dbi-link的东西,可以连接到oracle和mysql上。
开源cluster软件MysqlCluster可能与你的想象有较大差异。开源的cluster软件较少。复制(Replication)功能是异步的,并且有很大的局限性.例如,它是单线程的(single-threaded),因此一个处理能力更强的Slave的恢复速度也很难跟上处理能力相对较慢的Master.有丰富的开源cluster软件支持。
explainexplain看执行计划的结果简单。explain返回丰富的信息。
DDL是否有事务类似于ALTER TABLE或CREATE TABLE一类的操作都是非事务性的.它们会提交未提交的事务,并且不能回滚也不能做灾难恢复DDL也是有事务的。

二、pg二进制安装和编译安装。

总结pg二进制安装和编译安装

2.1、pg二进制安装

Rocky9 利用官方源安装 PostgreSQL-13.15

[root@rocky01 ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Last metadata expiration check: 0:02:50 ago on Thu Jun 13 06:44:42 2024.
pgdg-redhat-repo-latest.noarch.rpm                                                                                                                                                  9.0 kB/s |  12 kB     00:01    
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                                 Architecture                                  Version                                            Repository                                           Size
====================================================================================================================================================================================================================
Installing:
 pgdg-redhat-repo                                        noarch                                        42.0-43PGDG                                        @commandline                                         12 k

Transaction Summary
====================================================================================================================================================================================================================
Install  1 Package

Total size: 12 k
Installed size: 15 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                            1/1 
  Installing       : pgdg-redhat-repo-42.0-43PGDG.noarch                                                                                                                                                        1/1 
  Verifying        : pgdg-redhat-repo-42.0-43PGDG.noarch                                                                                                                                                        1/1 

Installed:
  pgdg-redhat-repo-42.0-43PGDG.noarch                                                                                                                                                                               

Complete!
[root@rocky01 ~]# yum -qy module disable postgresql
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
[root@rocky01 ~]# yum install -y postgresql13-server
Last metadata expiration check: 0:00:36 ago on Thu Jun 13 06:52:29 2024.
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                                   Architecture                                 Version                                                  Repository                                    Size
====================================================================================================================================================================================================================
Installing:
 postgresql13-server                                       x86_64                                       13.15-1PGDG.rhel9                                        pgdg13                                       5.6 M
Installing dependencies:
 postgresql13                                              x86_64                                       13.15-1PGDG.rhel9                                        pgdg13                                       1.4 M
 postgresql13-libs                                         x86_64                                       13.15-1PGDG.rhel9                                        pgdg13                                       779 k

Transaction Summary
====================================================================================================================================================================================================================
Install  3 Packages

Total download size: 7.8 M
Installed size: 32 M
Downloading Packages:
(1/3): postgresql13-13.15-1PGDG.rhel9.x86_64.rpm                                                                                                                                    748 kB/s | 1.4 MB     00:01    
(2/3): postgresql13-server-13.15-1PGDG.rhel9.x86_64.rpm                                                                                                                             2.2 MB/s | 5.6 MB     00:02    
(3/3): postgresql13-libs-13.15-1PGDG.rhel9.x86_64.rpm                                                                                                                               163 kB/s | 779 kB     00:04    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                               1.6 MB/s | 7.8 MB     00:04     
PostgreSQL 13 for RHEL / Rocky / AlmaLinux 9 - x86_64                                                                                                                               2.4 MB/s | 2.4 kB     00:00    
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                            1/1 
  Installing       : postgresql13-libs-13.15-1PGDG.rhel9.x86_64                                                                                                                                                 1/3 
  Running scriptlet: postgresql13-libs-13.15-1PGDG.rhel9.x86_64                                                                                                                                                 1/3 
  Installing       : postgresql13-13.15-1PGDG.rhel9.x86_64                                                                                                                                                      2/3 
  Running scriptlet: postgresql13-13.15-1PGDG.rhel9.x86_64                                                                                                                                                      2/3 
  Running scriptlet: postgresql13-server-13.15-1PGDG.rhel9.x86_64                                                                                                                                               3/3 
  Installing       : postgresql13-server-13.15-1PGDG.rhel9.x86_64                                                                                                                                               3/3 
  Running scriptlet: postgresql13-server-13.15-1PGDG.rhel9.x86_64                                                                                                                                               3/3 
  Verifying        : postgresql13-13.15-1PGDG.rhel9.x86_64                                                                                                                                                      1/3 
  Verifying        : postgresql13-libs-13.15-1PGDG.rhel9.x86_64                                                                                                                                                 2/3 
  Verifying        : postgresql13-server-13.15-1PGDG.rhel9.x86_64                                                                                                                                               3/3 

Installed:
  postgresql13-13.15-1PGDG.rhel9.x86_64                             postgresql13-libs-13.15-1PGDG.rhel9.x86_64                             postgresql13-server-13.15-1PGDG.rhel9.x86_64                            

Complete!
[root@rocky01 ~]# /usr/pgsql-13/bin/postgresql-13- initdb
postgresql-13-check-db-dir  postgresql-13-setup         
[root@rocky01 ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK

[root@rocky01 ~]# systemctl enable --now postgresql-13
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-13.service → /usr/lib/systemd/system/postgresql-13.service.
[root@rocky01 ~]# sudo -u postgres psql -c "SELECT version();"
could not change directory to "/root": Permission denied
                                                  version                                                  
-----------------------------------------------------------------------------------------------------------
 PostgreSQL 13.15 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.4.1 20231218 (Red Hat 11.4.1-3), 64-bit
(1 row)

[root@rocky01 ~]# su - postgres
[postgres@rocky01 ~]$ psql
psql (13.15)
Type "help" for help.

postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
postgres=# 

Rocky9 利用yum源安装 PostgreSQL-13.14

[root@rocky02 ~]# yum install -y postgresql-server
Rocky Linux 9 - BaseOS                                                                                                                                                               24 kB/s | 4.1 kB     00:00    
Rocky Linux 9 - BaseOS                                                                                                                                                              715 kB/s | 2.2 MB     00:03    
Rocky Linux 9 - AppStream                                                                                                                                                            38 kB/s | 4.5 kB     00:00    
Rocky Linux 9 - AppStream                                                                                                                                                           785 kB/s | 7.9 MB     00:10    
Rocky Linux 9 - Extras                                                                                                                                                               29 kB/s | 2.9 kB     00:00    
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                                      Architecture                                Version                                              Repository                                      Size
====================================================================================================================================================================================================================
Installing:
 postgresql-server                                            x86_64                                      13.14-1.el9_3                                        appstream                                      5.7 M
Installing dependencies:
 postgresql                                                   x86_64                                      13.14-1.el9_3                                        appstream                                      1.5 M
 postgresql-private-libs                                      x86_64                                      13.14-1.el9_3                                        appstream                                      132 k

Transaction Summary
====================================================================================================================================================================================================================
Install  3 Packages

Total download size: 7.4 M
Installed size: 29 M
Downloading Packages:
(1/3): postgresql-private-libs-13.14-1.el9_3.x86_64.rpm                                                                                                                             200 kB/s | 132 kB     00:00    
(2/3): postgresql-13.14-1.el9_3.x86_64.rpm                                                                                                                                          483 kB/s | 1.5 MB     00:03    
(3/3): postgresql-server-13.14-1.el9_3.x86_64.rpm                                                                                                                                   519 kB/s | 5.7 MB     00:11    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                               669 kB/s | 7.4 MB     00:11     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                            1/1 
  Installing       : postgresql-private-libs-13.14-1.el9_3.x86_64                                                                                                                                               1/3 
  Installing       : postgresql-13.14-1.el9_3.x86_64                                                                                                                                                            2/3 
  Running scriptlet: postgresql-server-13.14-1.el9_3.x86_64                                                                                                                                                     3/3 
  Installing       : postgresql-server-13.14-1.el9_3.x86_64                                                                                                                                                     3/3 
  Running scriptlet: postgresql-server-13.14-1.el9_3.x86_64                                                                                                                                                     3/3 
  Verifying        : postgresql-server-13.14-1.el9_3.x86_64                                                                                                                                                     1/3 
  Verifying        : postgresql-private-libs-13.14-1.el9_3.x86_64                                                                                                                                               2/3 
  Verifying        : postgresql-13.14-1.el9_3.x86_64                                                                                                                                                            3/3 

Installed:
  postgresql-13.14-1.el9_3.x86_64                                postgresql-private-libs-13.14-1.el9_3.x86_64                                postgresql-server-13.14-1.el9_3.x86_64                               

Complete!
[root@rocky02 ~]# /usr/bin/postgresql-setup --initdb
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@rocky02 ~]# systemctl enable --now  postgresql.service
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
[root@rocky02 ~]# su - postgres
[postgres@rocky02 ~]$ psql
psql (13.14)
Type "help" for help.

postgres=# 

Ubuntu利用官方源安装 PostgreSQL-13

root@ubuntu01:~# sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
root@ubuntu01:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
root@ubuntu01:~# sudo apt-get update
Get:1 http://apt.postgresql.org/pub/repos/apt jammy-pgdg InRelease [123 kB]                                               
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]                                                 
Get:3 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1509 kB]                               
Get:4 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [258 kB]                                                                                                                                
Get:5 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [1920 kB]                                                                                                                         
Get:6 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [326 kB]                                                                                                                          
Get:7 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [858 kB]                                                                                                                            
Get:8 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [166 kB]                                                                                                                            
Hit:9 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease                                                                                                                                                 
Get:10 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]                                                                                                                               
Get:11 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]                                                                                                                             
Get:12 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1723 kB]                                                                                                                    
Get:13 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 Packages [337 kB]                                                                                                                             
Get:14 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [317 kB]                                                                                                                     
Get:15 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [1977 kB]                                                                                                              
Get:16 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [336 kB]                                                                                                               
Get:17 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1086 kB]                                                                                                                
Get:18 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [251 kB]                                                                                                                 
Get:19 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [43.0 kB]                                                                                                              
Get:20 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [10.7 kB]                                                                                                              
Get:21 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [27.2 kB]                                                                                                              
Get:22 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [16.3 kB]                                                                                                              
Fetched 11.7 MB in 49s (236 kB/s)                                                                                                                                                                                  
Reading package lists... Done
W: http://apt.postgresql.org/pub/repos/apt/dists/jammy-pgdg/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
root@ubuntu01:~# sudo apt-get -y install postgresql-13
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  bridge-utils containerd dns-root-data dnsmasq-base pigz python3-docker python3-dockerpty python3-docopt python3-dotenv python3-texttable python3-websocket runc ubuntu-fan
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm15 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl postgresql-client-13 postgresql-client-common postgresql-common ssl-cert sysstat
Suggested packages:
  lm-sensors postgresql-doc-13 isag
The following NEW packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm15 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl postgresql-13 postgresql-client-13 postgresql-client-common postgresql-common ssl-cert
  sysstat
0 upgraded, 14 newly installed, 0 to remove and 81 not upgraded.
Need to get 43.9 MB of archives.
After this operation, 178 MB of additional disk space will be used.
Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libjson-perl all 4.04000-1 [81.8 kB]
Get:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 ssl-cert all 1.1.2 [17.4 kB]                                                                                                                   
Get:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libcommon-sense-perl amd64 3.75-2build1 [21.1 kB]                                                                                              
Get:4 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libtypes-serialiser-perl all 1.01-1 [11.6 kB]                                                                                                  
Get:5 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libjson-xs-perl amd64 4.030-1build3 [87.2 kB]                                                                                                  
Get:6 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libllvm15 amd64 1:15.0.7-0ubuntu0.22.04.3 [25.4 MB]                                                                                    
Get:7 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 postgresql-client-common all 260.pgdg22.04+1 [94.6 kB]                                                                                         
Get:8 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 postgresql-common all 260.pgdg22.04+1 [240 kB]                                                                                                 
Get:9 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 libpq5 amd64 16.3-1.pgdg22.04+1 [217 kB]                                                                                                       
Get:10 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 postgresql-client-13 amd64 13.15-1.pgdg22.04+1 [1508 kB]                                                                                      
Get:11 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 postgresql-13 amd64 13.15-1.pgdg22.04+1 [15.8 MB]                                                                                             
Get:12 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libsensors-config all 1:3.6.0-7ubuntu1 [5274 B]                                                                                               
Get:13 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libsensors5 amd64 1:3.6.0-7ubuntu1 [26.3 kB]                                                                                                  
Get:14 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 sysstat amd64 12.5.2-2ubuntu0.2 [487 kB]                                                                                              
Fetched 43.9 MB in 5min 28s (134 kB/s)                                                                                                                                                                             
Preconfiguring packages ...
Selecting previously unselected package libjson-perl.
(Reading database ... 94173 files and directories currently installed.)
Preparing to unpack .../00-libjson-perl_4.04000-1_all.deb ...
Unpacking libjson-perl (4.04000-1) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../01-postgresql-client-common_260.pgdg22.04+1_all.deb ...
Unpacking postgresql-client-common (260.pgdg22.04+1) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../02-ssl-cert_1.1.2_all.deb ...
Unpacking ssl-cert (1.1.2) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../03-postgresql-common_260.pgdg22.04+1_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Unpacking postgresql-common (260.pgdg22.04+1) ...
Selecting previously unselected package libcommon-sense-perl:amd64.
Preparing to unpack .../04-libcommon-sense-perl_3.75-2build1_amd64.deb ...
Unpacking libcommon-sense-perl:amd64 (3.75-2build1) ...
Selecting previously unselected package libtypes-serialiser-perl.
Preparing to unpack .../05-libtypes-serialiser-perl_1.01-1_all.deb ...
Unpacking libtypes-serialiser-perl (1.01-1) ...
Selecting previously unselected package libjson-xs-perl.
Preparing to unpack .../06-libjson-xs-perl_4.030-1build3_amd64.deb ...
Unpacking libjson-xs-perl (4.030-1build3) ...
Selecting previously unselected package libllvm15:amd64.
Preparing to unpack .../07-libllvm15_1%3a15.0.7-0ubuntu0.22.04.3_amd64.deb ...
Unpacking libllvm15:amd64 (1:15.0.7-0ubuntu0.22.04.3) ...
Selecting previously unselected package libpq5:amd64.
Preparing to unpack .../08-libpq5_16.3-1.pgdg22.04+1_amd64.deb ...
Unpacking libpq5:amd64 (16.3-1.pgdg22.04+1) ...
Selecting previously unselected package libsensors-config.
Preparing to unpack .../09-libsensors-config_1%3a3.6.0-7ubuntu1_all.deb ...
Unpacking libsensors-config (1:3.6.0-7ubuntu1) ...
Selecting previously unselected package libsensors5:amd64.
Preparing to unpack .../10-libsensors5_1%3a3.6.0-7ubuntu1_amd64.deb ...
Unpacking libsensors5:amd64 (1:3.6.0-7ubuntu1) ...
Selecting previously unselected package postgresql-client-13.
Preparing to unpack .../11-postgresql-client-13_13.15-1.pgdg22.04+1_amd64.deb ...
Unpacking postgresql-client-13 (13.15-1.pgdg22.04+1) ...
Selecting previously unselected package postgresql-13.
Preparing to unpack .../12-postgresql-13_13.15-1.pgdg22.04+1_amd64.deb ...
Unpacking postgresql-13 (13.15-1.pgdg22.04+1) ...
Selecting previously unselected package sysstat.
Preparing to unpack .../13-sysstat_12.5.2-2ubuntu0.2_amd64.deb ...
Unpacking sysstat (12.5.2-2ubuntu0.2) ...
Setting up postgresql-client-common (260.pgdg22.04+1) ...
Setting up libsensors-config (1:3.6.0-7ubuntu1) ...
Setting up libpq5:amd64 (16.3-1.pgdg22.04+1) ...
Setting up libcommon-sense-perl:amd64 (3.75-2build1) ...
Setting up postgresql-client-13 (13.15-1.pgdg22.04+1) ...
update-alternatives: using /usr/share/postgresql/13/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up ssl-cert (1.1.2) ...
Setting up libsensors5:amd64 (1:3.6.0-7ubuntu1) ...
Setting up libtypes-serialiser-perl (1.01-1) ...
Setting up libllvm15:amd64 (1:15.0.7-0ubuntu0.22.04.3) ...
Setting up libjson-perl (4.04000-1) ...
Setting up sysstat (12.5.2-2ubuntu0.2) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-collect.timer → /lib/systemd/system/sysstat-collect.timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-summary.timer → /lib/systemd/system/sysstat-summary.timer.
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up libjson-xs-perl (4.030-1build3) ...
Setting up postgresql-common (260.pgdg22.04+1) ...
Adding user postgres to group ssl-cert

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
'/etc/apt/trusted.gpg.d/apt.postgresql.org.gpg' -> '/usr/share/postgresql-common/pgdg/apt.postgresql.org.gpg'
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /lib/systemd/system/postgresql.service.
Setting up postgresql-13 (13.15-1.pgdg22.04+1) ...
Creating new PostgreSQL cluster 13/main ...
/usr/lib/postgresql/13/bin/initdb -D /var/lib/postgresql/13/main --auth-local peer --auth-host md5
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.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/postgresql/13/main ... 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:

    pg_ctlcluster 13 main start

Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
Scanning processes...                                                                                                                                                                                               
Scanning linux images...                                                                                                                                                                                            

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

#修改配置
postgres@ubuntu01:~$ vim /var/lib/postgresql/data/postgresql.conf 
listen_addresses = '*'

postgres@ubuntu01:~$ vim /var/lib/postgresql/data/pg_hba.conf 
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust 
host    all             all             0.0.0.0/0               md5

postgres@ubuntu01:~$ psql
psql (13.15 (Ubuntu 13.15-1.pgdg22.04+1))
Type "help" for help.

postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD '123456';
ALTER ROLE

Ubuntu利用系统源安装 PostgreSQL-13

root@ubuntu02:~# apt update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Get:2 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1509 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [258 kB]           
Get:4 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [1920 kB]                           
Get:5 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [326 kB]                                  
Get:6 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [858 kB]
Get:7 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [166 kB]
Hit:8 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease              
Get:9 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:10 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]
Get:11 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1723 kB]                                                                                                                    
Get:12 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [317 kB]                                                                                                                     
Get:13 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [1977 kB]                                                                                                              
Get:14 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [336 kB]                                                                                                               
Get:15 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1086 kB]                                                                                                                
Get:16 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [251 kB]                                                                                                                 
Get:17 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [43.0 kB]                                                                                                              
Get:18 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [10.7 kB]                                                                                                              
Get:19 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [27.2 kB]                                                                                                              
Get:20 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [16.3 kB]                                                                                                              
Fetched 11.2 MB in 50s (225 kB/s)                                                                                                                                                                                  
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
79 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@ubuntu02:~# apt install -y postgresql
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm14 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl postgresql-14 postgresql-client-14 postgresql-client-common postgresql-common ssl-cert
  sysstat
Suggested packages:
  lm-sensors postgresql-doc postgresql-doc-14 isag
The following NEW packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm14 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl postgresql postgresql-14 postgresql-client-14 postgresql-client-common
  postgresql-common ssl-cert sysstat
0 upgraded, 15 newly installed, 0 to remove and 79 not upgraded.
Need to get 42.4 MB of archives.
After this operation, 161 MB of additional disk space will be used.
Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libcommon-sense-perl amd64 3.75-2build1 [21.1 kB]
Get:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libjson-perl all 4.04000-1 [81.8 kB]
Get:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libtypes-serialiser-perl all 1.01-1 [11.6 kB]
Get:4 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libjson-xs-perl amd64 4.030-1build3 [87.2 kB]
Get:5 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libllvm14 amd64 1:14.0.0-1ubuntu1.1 [24.0 MB]
Get:6 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libpq5 amd64 14.12-0ubuntu0.22.04.1 [149 kB]                                                                                           
Get:7 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libsensors-config all 1:3.6.0-7ubuntu1 [5274 B]                                                                                                
Get:8 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 libsensors5 amd64 1:3.6.0-7ubuntu1 [26.3 kB]                                                                                                   
Get:9 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 postgresql-client-common all 238 [29.6 kB]                                                                                                     
Get:10 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 postgresql-client-14 amd64 14.12-0ubuntu0.22.04.1 [1223 kB]                                                                           
Get:11 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 ssl-cert all 1.1.2 [17.4 kB]                                                                                                                  
Get:12 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 postgresql-common all 238 [169 kB]                                                                                                            
Get:13 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 postgresql-14 amd64 14.12-0ubuntu0.22.04.1 [16.2 MB]                                                                                  
Get:14 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 postgresql all 14+238 [3288 B]                                                                                                                
Get:15 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 sysstat amd64 12.5.2-2ubuntu0.2 [487 kB]                                                                                              
Fetched 42.4 MB in 5min 19s (133 kB/s)                                                                                                                                                                             
Preconfiguring packages ...
Selecting previously unselected package libcommon-sense-perl:amd64.
(Reading database ... 93822 files and directories currently installed.)
Preparing to unpack .../00-libcommon-sense-perl_3.75-2build1_amd64.deb ...
Unpacking libcommon-sense-perl:amd64 (3.75-2build1) ...
Selecting previously unselected package libjson-perl.
Preparing to unpack .../01-libjson-perl_4.04000-1_all.deb ...
Unpacking libjson-perl (4.04000-1) ...
Selecting previously unselected package libtypes-serialiser-perl.
Preparing to unpack .../02-libtypes-serialiser-perl_1.01-1_all.deb ...
Unpacking libtypes-serialiser-perl (1.01-1) ...
Selecting previously unselected package libjson-xs-perl.
Preparing to unpack .../03-libjson-xs-perl_4.030-1build3_amd64.deb ...
Unpacking libjson-xs-perl (4.030-1build3) ...
Selecting previously unselected package libllvm14:amd64.
Preparing to unpack .../04-libllvm14_1%3a14.0.0-1ubuntu1.1_amd64.deb ...
Unpacking libllvm14:amd64 (1:14.0.0-1ubuntu1.1) ...
Selecting previously unselected package libpq5:amd64.
Preparing to unpack .../05-libpq5_14.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking libpq5:amd64 (14.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package libsensors-config.
Preparing to unpack .../06-libsensors-config_1%3a3.6.0-7ubuntu1_all.deb ...
Unpacking libsensors-config (1:3.6.0-7ubuntu1) ...
Selecting previously unselected package libsensors5:amd64.
Preparing to unpack .../07-libsensors5_1%3a3.6.0-7ubuntu1_amd64.deb ...
Unpacking libsensors5:amd64 (1:3.6.0-7ubuntu1) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../08-postgresql-client-common_238_all.deb ...
Unpacking postgresql-client-common (238) ...
Selecting previously unselected package postgresql-client-14.
Preparing to unpack .../09-postgresql-client-14_14.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking postgresql-client-14 (14.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../10-ssl-cert_1.1.2_all.deb ...
Unpacking ssl-cert (1.1.2) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../11-postgresql-common_238_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Unpacking postgresql-common (238) ...
Selecting previously unselected package postgresql-14.
Preparing to unpack .../12-postgresql-14_14.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking postgresql-14 (14.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package postgresql.
Preparing to unpack .../13-postgresql_14+238_all.deb ...
Unpacking postgresql (14+238) ...
Selecting previously unselected package sysstat.
Preparing to unpack .../14-sysstat_12.5.2-2ubuntu0.2_amd64.deb ...
Unpacking sysstat (12.5.2-2ubuntu0.2) ...
Setting up postgresql-client-common (238) ...
Setting up libsensors-config (1:3.6.0-7ubuntu1) ...
Setting up libpq5:amd64 (14.12-0ubuntu0.22.04.1) ...
Setting up libcommon-sense-perl:amd64 (3.75-2build1) ...
Setting up postgresql-client-14 (14.12-0ubuntu0.22.04.1) ...
update-alternatives: using /usr/share/postgresql/14/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up ssl-cert (1.1.2) ...
Setting up libsensors5:amd64 (1:3.6.0-7ubuntu1) ...
Setting up libllvm14:amd64 (1:14.0.0-1ubuntu1.1) ...
Setting up libtypes-serialiser-perl (1.01-1) ...
Setting up libjson-perl (4.04000-1) ...
Setting up sysstat (12.5.2-2ubuntu0.2) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-collect.timer → /lib/systemd/system/sysstat-collect.timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-summary.timer → /lib/systemd/system/sysstat-summary.timer.
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up libjson-xs-perl (4.030-1build3) ...
Setting up postgresql-common (238) ...
Adding user postgres to group ssl-cert

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /lib/systemd/system/postgresql.service.
Setting up postgresql-14 (14.12-0ubuntu0.22.04.1) ...
Creating new PostgreSQL cluster 14/main ...
/usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main --auth-local peer --auth-host scram-sha-256 --no-instructions
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.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/postgresql/14/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
update-alternatives: using /usr/share/postgresql/14/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Setting up postgresql (14+238) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...
Scanning processes...                                                                                                                                                                                               
Scanning linux images...                                                                                                                                                                                            

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host. 
root@ubuntu02:~# systemctl status postgresql.service 
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Thu 2024-06-13 07:50:51 UTC; 1min 49s ago
    Process: 3744 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 3744 (code=exited, status=0/SUCCESS)
        CPU: 2ms

Jun 13 07:50:51 ubuntu02 systemd[1]: Starting PostgreSQL RDBMS...
Jun 13 07:50:51 ubuntu02 systemd[1]: Finished PostgreSQL RDBMS.
root@ubuntu02:~# su - postgres 
postgres@ubuntu02:~$ psql
psql (14.12 (Ubuntu 14.12-0ubuntu0.22.04.1))
Type "help" for help.

postgres=# 


2.2、pg编译安装

#第一步:下载源代码
https://www.postgresql.org/ftp/source/
#第二步:编译安装。过程与Linux下其他软件的编译安装过程相同
./configure
make
make install
#第三步:编译安装完成后执行如下步骤 
#使用initdb命令初使用化数据库 
#启动数据库实例


三、pg相关命令及初始化操作

总结pg服务管理相关命令、pg_ct、和pgsql命令选项及示例和不同系统的初始化操作

3.1、pg服务管理相关命令

3.1.1、pg_ctl命令选项及示例

#查看服务状态
#查询数据库实例状态的命令如下:
pg_ctl status [-D datadir]


postgres@ubuntu01:~$ /usr/lib/postgresql/13/bin/pg_ctl status -D /var/lib/postgresql/13/main/
pg_ctl: server is running (PID: 5151)
/usr/lib/postgresql/13/bin/postgres "-D" "/var/lib/postgresql/13/main" "-c" "config_file=/etc/postgresql/13/main/postgresql.conf"

#启动PostgreSQL服务的命令:
pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]
#参数说明如下。
start #启动数据库实例
-w #等待启动完成
-t #等待启动完成的等待秒数,默认为60秒
-s #只打印错误和警告信息,不打印提示性信息
-D datadir#指定数据库实例的数据目录
-l #服务器日志输出附加在“filename”文件上,如果该文件不存在则创建它
-o options #声明要直接传递给postgres 的选项,具体可见postgres命令的帮助
-p path #指定postgres可执行文件的位置。默认情况下postgres可执行文件来自和pg_ctl相同的目录, 不必使用该选项。除非要进行一些不同寻常的操作,或者产生了postgres执行文件找不到的错误 -c #提高服务器的软限制(ulimit -c),尝试允许数据库实例在有异常时产生一个coredump文件,以便于问 题定位和故障分析

#停止PostgreSQL 数据库的命令:
pg_ctl stop [-w] [-t seconds] [-s] [-D datadir] [-m s[mart] l f[ast] | i [mmediate] ]
#参数说明如下。
-W #不等待数据库停下来,命令就返回。
-m #指定停止的模式。前面已叙述过停止的几种模式了。 #其它未说明的参数,其含义与启动数据库命令中的参数相同。

#重启PostgreSQL 数据库的命令
pg_ctl restart [-w] [-t seconds][-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o "options ]

#加载配置
#修改配置文件 postgresql.conf后,让修改生效的方法有两种
#方法一:在操作系统使用下面命令
pg_ctl reload [-s] [-D datadir]
#方法二:在 psql 中使用如下命令
postgres=# select pg_reload_conf();
#注意:加载配置操作只针对一些配置的修改生效,有些配置需要重新启动服务才能生效

3.3、pgsql命令选项及示例

#连接数据库
psql -h <hostname or ip> -p<端口> [数据库名称] -U [用户名称]
-h  #指定要连接的数据库主机名或IP地址,默认local socket登录(由配置项
unix_socket_directories指定)
-p  #指定连接的数据库端口
#最后两个参数是数据库名和用户名

#列出所有数据库
\l

#列出当前数据库的表
\dt

#退出psql
\q 或 quit

#帮助
\h

3.2、初始化操作

#rocky初始化数据库
[root@rocky01 ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb


#ubuntu初始化
[root@ubuntu2004 ~]#pg_createcluster 12 main --start


initdb -D /path/to/postgresql/data

四、pg数据库结构组织

总结pg数据库结构组织

实例:一个PostgreSQL对应一个安装的数据目录$PGDATA,既一个instance实例;
数据库:一个PostgreSQL数据库服务下可以管理多个数据库,当数据库连接到一个数据库时,一般只能访问这个数据库中的数据,而不能访问其他数据库中的内容,默认情况下初始实例只有三个数据库:postgres、template0、template1;
模式:一个数据库可以创建多个不同的名称空间即Schema,用于分隔不同的业务数据;
表和索引:一个数据库可以有多个表和索引,在PostgreSQL中表的术语称为Relation,而在其他数据库中通常叫Table;
行和列:每张表中有很多行和列数据,在PostgreSQL中行的术语一般为“Tuple”,而在其他数据库中则叫“Row”

五、pg远程连接。

实现pg远程连接。输入密码和无密码登陆

#修改配置
postgres@ubuntu01:~$ vim /var/lib/postgresql/data/postgresql.conf 
listen_addresses = '*'

postgres@ubuntu01:~$ vim /var/lib/postgresql/data/pg_hba.conf 
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust 
host    all             all             0.0.0.0/0               md5

postgres@ubuntu01:~$ psql
psql (13.15 (Ubuntu 13.15-1.pgdg22.04+1))
Type "help" for help.

postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD '123456';
ALTER ROLE

六、pg库、模式、表、数据操作。

总结库,模式,表的添加和删除操作。表数据的CURD。同时总结相关信息查看语句。

#数据库操作
postgres=# create database magedu;
CREATE DATABASE
postgres=# create database magedu_test;
CREATE DATABASE
postgres=# drop database magedu_test;
DROP DATABASE

#模式操作
postgres=# create schema magedu;
CREATE SCHEMA
postgres=# drop schema magedu;
DROP SCHEMA
postgres=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)

postgres=# \dt
Did not find any relations.

七、pg的用户和角色管理。

总结pg的用户和角色管理

创建用户
create user name [with] option
创建角色
create role name [with] optio
修改用户
alter user
删除用户
drop user
显示所有的用户和角色
\du

八、添加用户。

添加mage用户,magedu模式,准备zabbix库,配置mage用户的默认模式magedu,要求mage用户给zabbix库有所有权限。

#准备zabbix库
create database zabbix;
\c zabbix
create schema magedu;
#添加mage用户
create user mage;
#配置mage用户的默认模式magedu
alter schema magedu owner to mage;
#mage用户给zabbix库所有权限
alter database zabbix owner mage;

九、pgsql的进程结构。

总结pgsql的进程结构,说明进程间如何协同工作的。

Postmaster(master)进程:负责数据库启动和维护整个数据库实例的运行,当用户发起连接时,postmaster会和用户建立连接,然后创建子进程。当某个服务出现问题,postmaster会自动完成系统的修复。
Background writer 进程(BgWriter):负责在空闲时间写数据到磁盘,以减少事务提交时的开销。当往数据库中插入或更新数据时,并不会马上把数据持久化到数据文件中,而是先写入Buffer中,该辅助进程可以周期性的把内存中的脏数据刷新到磁盘中。
Checkpointer 进程:负责在事务提交时更新控制文件和日志文件,确保数据的一致性和恢复能力。
Wal sender 进程:负责在数据库集群中传递WAL(Write-Ahead Logging)文件给备服务器。
Statement/Query Executor 进程:执行SQL语句的进程。
Autovacuum worker 进程:负责自动清理死Tuple(即不再使用的旧版本数据)。
Logical replication launcher 进程:负责处理逻辑复制的进程。

十、pgsql数据目录结构。

总结pgsql的数据目录中结构,说明每个文件的作用,并可以配上一些示例说明文件的作用

初始化完成后,PGDATA数据目录下就会生成三个配置文件。
postgresql.conf #数据库实例的主配置文件,基本上所有的配置参数都在此文件中。 pg_hba.conf #认证配置文件,配置了允许哪些IP的主机访问数据库,认证的方法是什么等信息。

pg_ident.conf #认证方式ident的用户映射文件。

此外在PGDATA目录下还会生成如下一些子目录

base #默认表空间的目录,每个数据库都对应一个base目录下的子目录,每个表和索引对应一个独立文件 global #这个目录对应pg_global表空间,存放实例中的共享对象
pg_clog #存储事务提交状态数据
pg_bba.conf #数据库访问控制文件
pg_log #数据库系统日志目录,在查询一些系统错误时就可查看此目录下日志文件。(根据配置定义,可能没 有这个目录)
pg_xact #提交日志commit log的目录,pg 9之前叫pg_clog
pg_multixact #共享行锁的事务状态数据
pg_notify #异步消息相关的状态数据pg_serial #串行隔离级别的事务状态数据
pg_snapshots #存储执行了事务snapshot导出的状态数据pg_stat_tmp #统计信息的临时文件
pg_subtrans #子事务状态数据
pg_stat #统计信息的存储目录。关闭服务时,将pg_stat_tmp目录中的内容移动至此目录实现保存
pg_stat_tmp #统计信息的临时存储目录。开启数据库时存放统计信息
pg_tblsp #存储了指向各个用户自建表空间实际目录的链接文件
pg_twophase#使用两阶段提交功能时分布式事务的存储目录
pg_wal #WAL日志的目录,早期版一本目录为pg_xlog
PG_VERSION #数据库版本
postmaster.opts #记录数据库启动时的命令行选项
postmaster.pid #数据库启动的主进程信息文件,包括PID,SPGDATA目录,数据库启动时间,监听端口, socket文件路径,临听地址,共享内存的地址信息(ipsc可查看),主进程状态

  • 20
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值