PostgreSQL简单使用
一、PostgreSQL概念
特点
开源与自由
标准符合性
数据类型丰富
事务与并发
扩展性
安全性
优势
高性能
高可用性
灵活性
社区支持
成本效益
PostgreSQL结构
多层逻辑结构
第一层:实例(xxx.xxx.xxx.xxx:port)
第二层:数据库(每个实例下可有多个相互独立的数据库)
第三层:Schema(每个数据库下包含多个Schema)
实例就是指该数据库,对外表示的ip和端口。
数据库就是数据仓库。
schema是指模式,模式将多个用户之间相同名称的对象隔离开。
tablespace是指表空间,存放数据库的一个逻辑空间,可以存放不同的数据库,对应在物理层面上是一个目录。
关系型数据库
第二代数据库是关系型数据库
实体->关系
实体是指库中的表。 关系是指,比如两张表之间存在外键,这外键就是它俩之间的关系。
第三代数据库是面向对象型数据库
对象->关系
对象是指库、表、函数等等。 关系是指,比如在库中建了个表,表中建了个视图、索引等。
Oracle、PostgreSQL都属于第二代关系型数据库和第三代关系型数据库的结合。
PostgreSQL,一个表空间可以让多个数据库使用,一个数据库可以使用多个表空间。(多对多)
二、PostgreSQL安装示例
源码编译安装
1、安装依赖包
[root@localhost ~]# dnf -y install gcc* make libicu libicu-devel readline-devel zlib zlib-devel2、编译安装
[root@localhost ~]# ls
anaconda-ks.cfg postgresql-15.4.tar.gz
[root@localhost ~]# tar zxf postgresql-15.4.tar.gz
[root@localhost ~]# cd postgresql-15.4
[root@localhost postgresql-15.4]# ls
aclocal.m4 configure contrib doc HISTORY Makefile src
config configure.ac COPYRIGHT GNUmakefile.in INSTALL README
[root@localhost postgresql-15.4]# ./configure --prefix=/usr/local/pgsql
[root@localhost postgresql-15.4]# echo $?
0
[root@localhost postgresql-15.4]# make && make install
[root@localhost postgresql-15.4]# echo $?
03、配置环境变量
[root@localhost postgresql-15.4]# adduser postgres
[root@localhost postgresql-15.4]# mkdir /usr/local/pgsql/data
[root@localhost postgresql-15.4]# ll /usr/local/pgsql/
总计 20
drwxr-xr-x. 2 root root 4096 4月21日 09:19 bin
drwxr-xr-x. 2 root root 4096 4月21日 09:21 data
drwxr-xr-x. 6 root root 4096 4月21日 09:19 include
drwxr-xr-x. 4 root root 4096 4月21日 09:19 lib
drwxr-xr-x. 6 root root 4096 4月21日 09:19 share
[root@localhost postgresql-15.4]# ll /usr/local/
总计 44
drwxr-xr-x. 2 root root 4096 11月19日 22:13 bin
drwxr-xr-x. 2 root root 4096 11月19日 22:13 etc
drwxr-xr-x. 2 root root 4096 11月19日 22:13 games
drwxr-xr-x. 2 root root 4096 11月19日 22:13 include
drwxr-xr-x. 2 root root 4096 11月19日 22:13 lib
drwxr-xr-x. 3 root root 4096 3月 1日 17:26 lib64
drwxr-xr-x. 2 root root 4096 11月19日 22:13 libexec
drwxr-xr-x. 7 root root 4096 4月21日 09:21 pgsql
drwxr-xr-x. 2 root root 4096 11月19日 22:13 sbin
drwxr-xr-x. 5 root root 4096 3月 1日 17:26 share
drwxr-xr-x. 2 root root 4096 11月19日 22:13 src
[root@localhost postgresql-15.4]# chown postgres /usr/local/pgsql/data
[root@localhost postgresql-15.4]# ll /usr/local/pgsql/
总计 20
drwxr-xr-x. 2 root root 4096 4月21日 09:19 bin
drwxr-xr-x. 2 postgres root 4096 4月21日 09:21 data
drwxr-xr-x. 6 root root 4096 4月21日 09:19 include
drwxr-xr-x. 4 root root 4096 4月21日 09:19 lib
drwxr-xr-x. 6 root root 4096 4月21日 09:19 share
[root@localhost postgresql-15.4]# cat >> /etc/profile << 'EOF'
> LD_LIBRARY_PATH=/usr/local/pgsql/lib # 配置共享库
> export LD_LIBRARY_PATH
> PATH=/usr/local/pgsql/bin:$PATH # 配置命令可搜索路径
> export PATH
> EOF
[root@localhost postgresql-15.4]# source /etc/profile4、登录数据库
[root@localhost postgresql-15.4]# su - postgres # 以postgres用户的来完成后续的操作。[postgres@localhost ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
# 用于初始化PostgreSQL数据库目录和数据文件。
# -D选项指定了数据库的数据目录。
# initdb是postgresql的内部命令,用来创建并初始化一个新的postgresql数据库。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 "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".Data page checksums are disabled.fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start[postgres@localhost ~]$ echo $?
0
[postgres@localhost ~]$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
# -D /usr/local/pgsql/data指定了数据目录的位置,与上条命令相同。
# start指示postgresql开始运行。
waiting for server to start.... done
server started
[postgres@localhost ~]$ echo $?
0
[postgres@localhost ~]$ /usr/local/pgsql/bin/psql
psql (15.4)
Type "help" for help.postgres=# [root@localhost ~]# dnf -y install tree[root@localhost ~]# tree -L 1 -d /usr/local/pgsql/data/
/usr/local/pgsql/data/
├── base
├── global
├── pg_commit_ts
├── pg_dynshmem
├── pg_logical
├── pg_multixact
├── pg_notify
├── pg_replslot
├── pg_serial
├── pg_snapshots
├── pg_stat
├── pg_stat_tmp
├── pg_subtrans
├── pg_tblspc
├── pg_twophase
├── pg_wal
└── pg_xact18 directories[root@localhost pgsql]# cd data
[root@localhost data]# ls
base pg_ident.conf pg_serial pg_tblspc postgresql.auto.conf
global pg_logical pg_snapshots pg_twophase postgresql.conf
pg_commit_ts pg_multixact pg_stat PG_VERSION postmaster.opts
pg_dynshmem pg_notify pg_stat_tmp pg_wal postmaster.pid
pg_hba.conf pg_replslot pg_subtrans pg_xact
[root@localhost data]# cd base
[root@localhost base]# ls
1 4 5 # 每个数据库都会在$PGDATA/base下面生成一个子目录
[root@localhost base]# cd 1
[root@localhost 1]# ls
112 2337 2616_vm 2686 3079_vm 3596 4157
113 2579 2617 2687 3080 3597 4158
1247 2600 2617_fsm 2688 3081 3598 4159
1247_fsm 2600_fsm 2617_vm 2689 3085 3599 4160
-----略postgres=# select datname,oid from pg_database;datname | oid
-----------+-----postgres | 5template1 | 1template0 | 4
(3 rows)
# 与上面的物理文件一一对应。yum安装
[root@localhost ~]# dnf -y install postgresql-server
[root@localhost ~]# postgresql-setup --initdb # 初始化数据库。* Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ ls
backups data initdb_postgresql.log
[postgres@localhost ~]$ cat initdb_postgresql.log
属于此数据库系统的文件宿主为用户 "postgres".
此用户也必须为服务器进程的宿主.
数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
默认的数据库编码已经相应的设置为 "UTF8".
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
缺省的文本搜索配置将会被设置到"simple"禁止为数据页生成校验和.修复已存在目录 /var/lib/pgsql/data 的权限 ... 成功
正在创建子目录 ... 成功
选择动态共享内存实现 ......posix
选择默认最大联接数 (max_connections) ... 100
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择默认时区 ... Asia/Shanghai
创建配置文件 ... 成功
正在运行自举脚本 ...成功
正在执行自举后初始化 ...成功
同步数据到磁盘...成功成功。您现在可以用下面的命令开启数据库服务器:/usr/bin/pg_ctl -D /var/lib/pgsql/data -l 日志文件 start[postgres@localhost ~]$ /usr/bin/pg_ctl -D /var/lib/pgsql/data -l initdb_postgresql.log start
等待服务器进程启动 .... 完成
服务器进程已经启动
[postgres@localhost ~]$ ls
backups data initdb_postgresql.log
[postgres@localhost ~]$ cat initdb_postgresql.log
属于此数据库系统的文件宿主为用户 "postgres".
此用户也必须为服务器进程的宿主.
数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
默认的数据库编码已经相应的设置为 "UTF8".
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
缺省的文本搜索配置将会被设置到"simple"禁止为数据页生成校验和.修复已存在目录 /var/lib/pgsql/data 的权限 ... 成功
正在创建子目录 ... 成功
选择动态共享内存实现 ......posix
选择默认最大联接数 (max_connections) ... 100
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择默认时区 ... Asia/Shanghai
创建配置文件 ... 成功
正在运行自举脚本 ...成功
正在执行自举后初始化 ...成功
同步数据到磁盘...成功成功。您现在可以用下面的命令开启数据库服务器:/usr/bin/pg_ctl -D /var/lib/pgsql/data -l 日志文件 start2025-03-01 11:46:22.784 CST [2368] 日志: 日志输出重定向到日志收集进程
2025-03-01 11:46:22.784 CST [2368] 提示: 后续的日志输出将出现在目录 "log"中.
[postgres@localhost ~]$ psql
psql (15.12)
输入 "help" 来获取帮助信息.postgres=# exit
[postgres@localhost ~]$ psql
psql (15.12)
输入 "help" 来获取帮助信息.postgres=#