PostgreSQL 目录结构与配置文件 postgresql.conf 介绍
一般使用环境变量PGDATA指向数据目录的根目录,该目录在安装时指定。
查看环境变量
[postgres@pgserver pgdata]$ cat ~/.bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/.local/bin:$HOME/binexport PGPORT=5432export PGDATA=/postgresql/pgdataexport PGHOME=/postgresql/pg16export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATHexport PATH=$PGHOME/bin:$PATH:export PGHOST=$PGDATAexport PGUSER=postgresexport PGDATABASE=postgres
软件安装目录
[postgres@pgserver postgresql]$ lltotal 8drwxrwxr-x. 2 postgres postgres 4096 Oct 29 20:30 archivedrwxrwxr-x. 2 postgres postgres 6 Sep 11 23:36 backupdrwxrwxr-x. 6 postgres postgres 56 Sep 11 23:51 pg16drwx------. 21 postgres postgres 4096 Oct 29 20:30 pgdatadrwxrwxr-x. 2 postgres postgres 6 Sep 11 23:36 scriptsdrwxr-xr-x. 3 postgres postgres 59 Sep 11 23:45 soft[postgres@pgserver pg16]$ pwd/postgresql/pg16[postgres@pgserver pg16]$ lltotal 16drwxrwxr-x. 2 postgres postgres 4096 Sep 11 23:54 bindrwxrwxr-x. 6 postgres postgres 4096 Sep 11 23:54 includedrwxrwxr-x. 4 postgres postgres 4096 Sep 11 23:54 libdrwxrwxr-x. 8 postgres postgres 4096 Sep 11 23:54 share[postgres@pgserver pg16]$ cd bin[postgres@pgserver bin]$ lltotal 14512-rwxr-xr-x. 1 postgres postgres 83944 Sep 11 23:54 clusterdb-rwxr-xr-x. 1 postgres postgres 88232 Sep 11 23:54 createdb-rwxr-xr-x. 1 postgres postgres 88640 Sep 11 23:54 createuser-rwxr-xr-x. 1 postgres postgres 79088 Sep 11 23:54 dropdb-rwxr-xr-x. 1 postgres postgres 79024 Sep 11 23:54 dropuser-rwxr-xr-x. 1 postgres postgres 1029520 Sep 11 23:54 ecpg-rwxr-xr-x. 1 postgres postgres 165976 Sep 11 23:54 initdb-rwxr-xr-x. 1 postgres postgres 54000 Sep 11 23:54 oid2name-rwxr-xr-x. 1 postgres postgres 115200 Sep 11 23:54 pg_amcheck-rwxr-xr-x. 1 postgres postgres 48576 Sep 11 23:54 pg_archivecleanup-rwxr-xr-x. 1 postgres postgres 169024 Sep 11 23:54 pg_basebackup-rwxr-xr-x. 1 postgres postgres 211576 Sep 11 23:54 pgbench-rwxr-xr-x. 1 postgres postgres 84304 Sep 11 23:54 pg_checksums-rwxr-xr-x. 1 postgres postgres 42928 Sep 11 23:54 pg_config-rwxr-xr-x. 1 postgres postgres 61664 Sep 11 23:54 pg_controldata-rwxr-xr-x. 1 postgres postgres 77208 Sep 11 23:54 pg_ctl-rwxr-xr-x. 1 postgres postgres 435296 Sep 11 23:54 pg_dump-rwxr-xr-x. 1 postgres postgres 136912 Sep 11 23:54 pg_dumpall-rwxr-xr-x. 1 postgres postgres 78680 Sep 11 23:54 pg_isready-rwxr-xr-x. 1 postgres postgres 116272 Sep 11 23:54 pg_receivewal-rwxr-xr-x. 1 postgres postgres 116040 Sep 11 23:54 pg_recvlogical-rwxr-xr-x. 1 postgres postgres 71880 Sep 11 23:54 pg_resetwal-rwxr-xr-x. 1 postgres postgres 216112 Sep 11 23:54 pg_restore-rwxr-xr-x. 1 postgres postgres 169128 Sep 11 23:54 pg_rewind-rwxr-xr-x. 1 postgres postgres 54192 Sep 11 23:54 pg_test_fsync-rwxr-xr-x. 1 postgres postgres 43344 Sep 11 23:54 pg_test_timing-rwxr-xr-x. 1 postgres postgres 175608 Sep 11 23:54 pg_upgrade-rwxr-xr-x. 1 postgres postgres 123904 Sep 11 23:54 pg_verifybackup-rwxr-xr-x. 1 postgres postgres 123224 Sep 11 23:54 pg_waldump-rwxr-xr-x. 1 postgres postgres 9487600 Sep 11 23:54 postgres-rwxr-xr-x. 1 postgres postgres 712464 Sep 11 23:54 psql-rwxr-xr-x. 1 postgres postgres 93088 Sep 11 23:54 reindexdb-rwxr-xr-x. 1 postgres postgres 97800 Sep 11 23:54 vacuumdb-rwxr-xr-x. 1 postgres postgres 53728 Sep 11 23:54 vacuumlo[postgres@pgserver bin]$
数据目录
[postgres@pgserver postgresql]$ cd pgdata/[postgres@pgserver pgdata]$ pwd/postgresql/pgdata[postgres@pgserver pgdata]$ lltotal 68drwx------. 5 postgres postgres 33 Sep 11 23:59 base-rw------- 1 postgres postgres 47 Oct 29 20:30 current_logfilesdrwx------. 2 postgres postgres 4096 Oct 29 20:31 globaldrwx------. 2 postgres postgres 25 Sep 12 09:23 logdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_commit_tsdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_dynshmem-rw-------. 1 postgres postgres 5760 Sep 12 00:11 pg_hba.conf-rw-------. 1 postgres postgres 2640 Sep 11 23:59 pg_ident.confdrwx------ 2 postgres postgres 326 Oct 29 20:30 pg_logdrwx------. 4 postgres postgres 68 Oct 29 20:35 pg_logicaldrwx------. 4 postgres postgres 36 Sep 11 23:59 pg_multixactdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_notifydrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_replslotdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_serialdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_snapshotsdrwx------. 2 postgres postgres 6 Oct 29 20:30 pg_statdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_stat_tmpdrwx------. 2 postgres postgres 18 Sep 11 23:59 pg_subtransdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_tblspcdrwx------. 2 postgres postgres 6 Sep 11 23:59 pg_twophase-rw-------. 1 postgres postgres 3 Sep 11 23:59 PG_VERSIONdrwx------. 3 postgres postgres 92 Oct 29 20:35 pg_waldrwx------. 2 postgres postgres 18 Sep 11 23:59 pg_xact-rw-------. 1 postgres postgres 88 Sep 11 23:59 postgresql.auto.conf-rw------- 1 postgres postgres 29713 Sep 12 15:46 postgresql.conf-rw-------. 1 postgres postgres 30 Oct 29 20:30 postmaster.opts-rw------- 1 postgres postgres 90 Oct 29 20:30 postmaster.pid
文件目录说明:
base:#默认表空间的目录,该目录包含数据库用户所创建的各个数据库,同时也包括postgres、template0和template1的pg_defaulttablespace。global:#该目录包含集群范围的各个表和相关视图。(pg_database、pg_tablespace)pg_dynshmem:#该目录包含动态共享内存子系统使用的文件。pg_hba.conf:#认证配置文件,配置了允许哪些IP访问数据库,及认证方式等信息。pg_commit_ts:#该目录包含已提交事务的时间。pg_ident.conf:#"ident"认证方式的用户映射文件。pg_log:#日志目录,自定义pg_logical:#该目录包含逻辑解码的状态数据。pg_multixact:#该目录包含多事务状态数据。(等待锁定的并发事务)pg_notify:#该目录包含LISTEN/NOTIFY状态数据。pg_replslot:#该目录包含复制槽数据。pg_serial:pg_snapshots:#该目录包含导出的快照。pg_stat:#该目录包含统计子系统的永久文件。pg_stat_tmp:#该目录包含统计子系统的临时文件。pg_subtrans:#该目录包含子事务状态数据。pg_tblspc:#该目录包含表空间的符号链接。pg_twophase:#该目录包含预备事务的状态文件。PG_VERSION:#存储PostgreSQL主版本号。pg_wal:#该目录包含wal日志。pg_xact:#该目录包含事务提交状态数据。postgresql.auto.conf:#作用同 postgresql.conf ,优先级高于 postgresql.conf,在数据库中通过alter命令更改的参数记录在此文件中。postgresql.conf:#数据库实例主配置文件,基本上所有的数据库参数配置都在此文件中。postmaster.opts:#记录数据库启动命令。postmaster.pid:#数据库进程文件,数据库启动时被创建,关闭时消失。
默认表空间base目录下有很多子目录,这些子目录的名称与相应数据库的OID相同。
[postgres@pgserver postgresql]$ psqlpsql (16.4)Type "help" for help.postgres =# select oid,datname from pg_database;oid | datname-----+-----------5 | postgres1 | template14 | template0(3 rows)[postgres@pgserver base]$ lltotal 36drwx------. 2 postgres postgres 8192 Sep 13 14:50 1drwx------. 2 postgres postgres 8192 Sep 11 23:59 4drwx------. 2 postgres postgres 8192 Oct 29 20:31 5
postgres数据库的oid为5,则它对应的子目录名称就是5。在5目录下,存放着postgres数据库的表、索引等数据文件。每个表或索引都会分配一个文件号 relfilenode,数据文件格式则以“<relfilenode>[.顺序号]”命名,每个文件最大为1GB,当表或索引的内容大于1GB时,就会从1开始生成顺序号。所以一张表的数据文件的路径为:
<默认表空间的目录>/<database oid> <relfilenode>[.顺序号],而一张表或索引的“relfilenode”是记录在系统表pg_class的 relfilenode字段中的。
PostgreSQL中最大的逻辑存储单位是表空间,数据库中创建的对象都保存在表空间中,例如表、索引和整个数据库都可以被分配到特定的表空间。在创建数据库对象时,可以指定数据库对象的表空间,如果不指定则使用默认表空间,也就是数据库对象的文件的位置。初始化数据库目录时会自动创建pg_default和pg_global两个表空间。如下所示:
命令:\db
postgres-# \cYou are now connected to database "postgres" as user "postgres".postgres-# \dbList of tablespacesName | Owner | Location------------+----------+----------pg_default | postgres |pg_global | postgres |(2 rows)
数据库中的表、索引、序列等对象的OID存储在pg_class系统表中,可以通过如下代码查询获得这些对象的OID:
select oid,relname,relkind from pg_class where relname ~'test';
更多关于PostgreSQL 系列的学习文章后期持续更新中,下期见。
文章转载自DBA随笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




