本章为PostgreSQL简介及如何用psql和pgAdmin GUI连接PostgreSQL。
开源,低TCO,30多年持续开发,符合SQL:2023标准,高度可扩展,多模。
PostgreSQL 的功能集与 Oracle 或 SQL Server 的相似度比与 MySQL 更高。
PostgreSQL知名用户包括苹果、巴斯夫、基因泰克、Heroku、IMDB、Skype、迈克菲、NTT、英国气象局和美国国家气象局。
支持事务,时间点恢复,同步复制(5个9可用性)。
支持基于角色的访问控制,RLS,SCRAM 256位保护,原生加密函数(pgcrypto),函数的defeiner权限。安全信息请参考这里。
很容易获得文档,驱动和接口。
文档也可下载到本地:postgresql-doc-16。
支持最大1GB 文本数据。
支持extension,如PostGIS。
支持用户自定义数据类型,操作符,索引,函数和语言。
optimizer,MVCC,HTAP。
PostgreSQL 16 在 4 插槽服务器上可以实现每秒超过 1,000,000 次读取,并且其基准测试结果(pgbench?)为每秒超过 50,000 次写入事务。
此文介绍了如何做benchmark。
此视频比较了PostgreSQL和MySQL的benchmark。
多CPU,热备。
支持关系型,JSON(jsonb),键值对(hstore),支持全文搜索。
MySQL的被收购促进了PG使用的增长。
专业服务厂商如EDB,及通过公有云厂商。详见这里。
重点提了ALXE项目。
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.
特此授予以任何目的免费且无书面协议使用、复制、修改和分发本软件及其文档的许可,但前提是上述版权声明和本段及以下两段出现在所有副本中。
从这里下载。
开源软件如PG以社区为基础运作。
除PG核心,还有很多extension和周边工具。
直连,连接池后续章节讲。
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".
两项工作:
详见博客pgAdmin 4 连接 postgreSQL。
peer认证相对于oracle的OS认证,前提是登录数据库的用户名在OS中已存在。
从这里下载。
支持执行计划和运行psql命令。
详见博客pgAdmin 4 连接 postgreSQL。
其他管理/开发工具参见软件目录,或所有软件。
常用选项:
psql一个命令中可以有多个-c和-f选项并存。
psql支持SQL和psql元命令。分别通过\h
和\?
获得帮助。
SQL的单行注释用-- comment
,多行注释用/* comment */
口令加密默认也是推荐:
postgres=# show password_encryption;
password_encryption
---------------------
scram-sha-256
(1 row)
改口令可用passwd元命令或ALTER USER username PASSWORD password。
不建议,因还是把密码写在文件中,详见这里。
pg_service.conf,类似于oracle的tnsnames.ora,详见这里。
看日志。
监听设置,权限设置,防火墙设置…。
$ 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)
详见这里。
这里以EDB的公有云服务BigAnimal为例。
Aiven,AWS,Crunchy,Google和Microsoft也提供PostgreSQL云服务。
官方发布的docker见这里,支持的版本见这里。
TPA(Trusted Postgres Architect)是EDB的工具,此略。