PostgreSQL 16 Administration Cookbook 读书笔记:第1章 First Steps

本章为PostgreSQL简介及如何用psql和pgAdmin GUI连接PostgreSQL。

1. PostgreSQL 16 简介

开源,低TCO,30多年持续开发,符合SQL:2023标准,高度可扩展,多模。

1.1 PostgreSQL 有何不同?

PostgreSQL 的功能集与 Oracle 或 SQL Server 的相似度比与 MySQL 更高。

PostgreSQL知名用户包括苹果、巴斯夫、基因泰克、Heroku、IMDB、Skype、迈克菲、NTT、英国气象局和美国国家气象局。

1.1.1 稳健性

支持事务,时间点恢复,同步复制(5个9可用性)。

1.1.2 安全性

支持基于角色的访问控制,RLS,SCRAM 256位保护,原生加密函数(pgcrypto),函数的defeiner权限。安全信息请参考这里。

1.1.3 易用性

很容易获得文档,驱动和接口。

文档也可下载到本地:postgresql-doc-16。

支持最大1GB 文本数据。

1.1.4 可扩展性

支持extension,如PostGIS。
支持用户自定义数据类型,操作符,索引,函数和语言。

1.1.5 性能和并发性

optimizer,MVCC,HTAP。

PostgreSQL 16 在 4 插槽服务器上可以实现每秒超过 1,000,000 次读取,并且其基准测试结果(pgbench?)为每秒超过 50,000 次写入事务。

此文介绍了如何做benchmark。

此视频比较了PostgreSQL和MySQL的benchmark。

1.1.6 可扩展性

多CPU,热备。

1.1.7 SQL 和 NoSQL 数据模型

支持关系型,JSON(jsonb),键值对(hstore),支持全文搜索。

1.1.8 流行度

MySQL的被收购促进了PG使用的增长。

1.1.9 商业支持

专业服务厂商如EDB,及通过公有云厂商。详见这里。

1.1.10 研发资金

重点提了ALXE项目。

2. 如何获取 PostgreSQL

100%开源。许可见这里。

Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.
 
特此授予以任何目的免费且无书面协议使用、复制、修改和分发本软件及其文档的许可,但前提是上述版权声明和本段及以下两段出现在所有副本中。

2.1 如何操作……

从这里下载。

2.2 如何运作……

开源软件如PG以社区为基础运作。

2.3 更多内容……

除PG核心,还有很多extension和周边工具。

3. 连接到 PostgreSQL 服务器

直连,连接池后续章节讲。

3.1 准备工作

libpq 是一组库函数,允许客户端程序将查询传递给 PostgreSQL。C,C++, Perl, Python, Tcl 和 ECPG都用他。

注意是libpq而非libqg,表示Postgres Query。

psql的连接信息可以用命令行选项或URI指定,详见man page:

$ psql "service=myservice sslmode=require"
$ psql postgresql://dbmaster:5433/mydb?sslmode=require

例如:

$ psql postgresql://localhost:5432/sampledb?user=dbadmin
Password for user dbadmin:
psql (16.9)
Type "help" for help.

sampledb=>

PG在IANA注册的端口号为5432:

$ grep postgres /etc/services
postgres        5432/tcp        postgresql      # POSTGRES
postgres        5432/udp        postgresql      # POSTGRES

PG术语中,database server即database cluster,包含多个数据库。

获取更多信息:

sampledb=> select version();
                                                 version
----------------------------------------------------------------------------------------------------------
 PostgreSQL 16.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-5), 64-bit
(1 row)

sampledb=> select current_database(), current_user;
 current_database | current_user
------------------+--------------
 sampledb         | dbadmin
(1 row)

sampledb=> select inet_server_addr(), inet_server_port();
 inet_server_addr | inet_server_port
------------------+------------------
 ::1              |             5432
(1 row)

sampledb=> \conninfo
You are connected to database "sampledb" as user "dbadmin" on host "localhost" (address "::1") at port "5432".

4. 启用网络/远程用户访问

两项工作:

  • 监听,修改postgresql.conf中的listen_addresses
  • ACL,修改pg_hba.conf

详见博客pgAdmin 4 连接 postgreSQL。

peer认证相对于oracle的OS认证,前提是登录数据库的用户名在OS中已存在。

5. 使用 pgAdmin 4 GUI 工具

从这里下载。
支持执行计划和运行psql命令。

详见博客pgAdmin 4 连接 postgreSQL。

其他管理/开发工具参见软件目录,或所有软件。

6. 使用 psql 查询和脚本工具

常用选项:

  • -c, --command=COMMAND run only single command (SQL or internal) and exit
  • -f, --file=FILENAME execute commands from file, then exit

psql一个命令中可以有多个-c和-f选项并存。

psql支持SQL和psql元命令。分别通过\h\?获得帮助。

SQL的单行注释用-- comment,多行注释用/* comment */

7. 安全地更改密码

口令加密默认也是推荐:

postgres=# show password_encryption;
 password_encryption
---------------------
 scram-sha-256
(1 row)

改口令可用passwd元命令或ALTER USER username PASSWORD password

8. 避免硬编码密码

不建议,因还是把密码写在文件中,详见这里。

9. 使用连接服务文件

pg_service.conf,类似于oracle的tnsnames.ora,详见这里。

10. 连接失败故障排除

看日志。

监听设置,权限设置,防火墙设置…。

$ pg_isready
/run/postgresql:5432 - accepting connections
$ pg_ctl stop
waiting for server to shut down...................... done
server stopped
$ pg_isready
/run/postgresql:5432 - no response

一些开关:

postgres=# show log_connections;
 log_connections
-----------------
 off
(1 row)

postgres=# show log_disconnections;
 log_disconnections
--------------------
 off
(1 row)

详见这里。

11. 云端 PostgreSQL

这里以EDB的公有云服务BigAnimal为例。

Aiven,AWS,Crunchy,Google和Microsoft也提供PostgreSQL云服务。

12. PostgreSQL 与 Kubernetes

官方发布的docker见这里,支持的版本见这里。

13. PostgreSQL 与 TPA

TPA(Trusted Postgres Architect)是EDB的工具,此略。

你可能感兴趣的:(PostgreSQL 16 Administration Cookbook 读书笔记:第1章 First Steps)