在一些场景下,需要在一台服务器上运行多个数据库实例,可以通过cluster_name参数设置进程的标示,这样的话在系统中查看多个运行的PG实例的时候,可以通过进程前的cluster_name区分,以便进行分析或者维护操作。体验一下cluster_name的魅力:修改参数之前,系统中看到的进程均没有前缀,目前系统中只运行了1个PostgreSQL数据库实例:[root@dbserver tmp]# ps -ef|grep postgrespostgres  3335     1  0 17:59 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/datapostgres  3337  3335  0 17:59 ?        00:00:00 postgres: logger process                                postgres  3339  3335  0 17:59 ?        00:00:00 postgres: checkpointer process                          postgres  3340  3335  0 17:59 ?        00:00:00 postgres: writer process                                postgres  3341  3335  0 17:59 ?        00:00:00 postgres: wal writer process                            postgres  3342  3335  0 17:59 ?        00:00:00 postgres: autovacuum launcher process                   postgres  3343  3335  0 17:59 ?        00:00:00 postgres: stats collector process                       root      3487  3390  0 18:03 pts/0    00:00:00 grep postgres修改配置文件postgresql.conf中的cluster_name参数:改成了如下:cluster_name = 'eric'                   # added to process titles if nonempty然后保存postgresql.conf,重启数据库查看一下进程名称已经有了cluster name:[root@dbserver pg_log]# service postgresql-9.5 restart停止 postgresql-9.5 服务:                                 [确定]启动 postgresql-9.5 服务:                                 [确定][root@dbserver pg_log]# ps -ef|grep postgrespostgres  5246     1  0 18:17 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/datapostgres  5248  5246  0 18:17 ?        00:00:00 postgres: eric: logger process                          postgres  5250  5246  0 18:17 ?        00:00:00 postgres: eric: checkpointer process                    postgres  5251  5246  0 18:17 ?        00:00:00 postgres: eric: writer process                          postgres  5252  5246  0 18:17 ?        00:00:00 postgres: eric: wal writer process                      postgres  5253  5246  0 18:17 ?        00:00:00 postgres: eric: autovacuum launcher process             postgres  5254  5246  0 18:17 ?        00:00:00 postgres: eric: stats collector process                 root      5259  4945  0 18:17 pts/0    00:00:00 grep postgres进程的前面已经有了cluster_name前缀。多个实例运行的时候,该功能效果比较明显,我们再创建一个实例并且设置一下cluster_name:创建第2个实例的目录:[root@dbserver /]# mkdir pgdata2[root@dbserver /]# chown -R postgres.postgres /pgdata2创建新的实例:-bash-3.2$ cd /usr/pgsql-9.5/bin/-bash-3.2$ ./initdb -D /pgdata2属于此数据库系统的文件宿主为用户 "postgres".此用户也必须为服务器进程的宿主.数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.默认的数据库编码已经相应的设置为 "UTF8".initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置缺省的文本搜索配置将会被设置到"simple"禁止为数据页生成校验和.修复已存在目录 /pgdata2 的权限 ... 成功正在创建子目录 ... 成功选择默认最大联接数 (max_connections) ... 100选择默认共享缓冲区大小 (shared_buffers) ... 128MB选择动态共享内存实现 ......posix创建配置文件 ... 成功在 /pgdata2/base/1 中创建 template1 数据库 ... 成功初始化 pg_authid ...  成功初始化dependencies ... 成功创建系统视图 ... 成功正在加载系统对象描述 ...成功创建(字符集)校对规则 ... 成功创建字符集转换 ... 成功正在创建字典 ... 成功对内建对象设置权限 ... 成功创建信息模式 ... 成功正在装载PL/pgSQL服务器端编程语言...成功清理数据库 template1 ... 成功拷贝 template1 到 template0 ... 成功拷贝 template1 到 template0 ... 成功同步数据到磁盘...成功警告:为本地连接启动了 "trust" 认证.你可以通过编辑 pg_hba.conf 更改或你下次行 initdb 时使用 -A或者--auth-local和--auth-host选项.Success. You can now start the database server using:    ./pg_ctl -D /pgdata2 -l logfile start改一下新实例的端口:-bash-3.2$ cd /pgdata2-bash-3.2$ lsbase          pg_dynshmem    pg_multixact  pg_snapshots  pg_tblspc    postgresql.auto.confglobal        pg_hba.conf    pg_notify     pg_stat       pg_twophase  postgresql.confpg_clog       pg_ident.conf  pg_replslot   pg_stat_tmp   PG_VERSIONpg_commit_ts  pg_logical     pg_serial     pg_subtrans   pg_xlog-bash-3.2$ vi postgresql.conf改成:port = 5433启动新实例:-bash-3.2$ ./pg_ctl -D /pgdata2 start正在启动服务器进程-bash-3.2$ 日志:  日志输出重定向到日志收集进程提示:  后续的日志输出将出现在目录 "pg_log"中.查看一下系统中2个实例的进程标示有什么不同:-bash-3.2$ ps -ef|grep postgrespostgres  5393     1  0 18:25 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /var/lib/pgsql/9.5/datapostgres  5394  5393  0 18:25 ?        00:00:00 postgres: eric: logger process                        postgres  5396  5393  0 18:25 ?        00:00:00 postgres: eric: checkpointer process                  postgres  5397  5393  0 18:25 ?        00:00:00 postgres: eric: writer process                        postgres  5398  5393  0 18:25 ?        00:00:00 postgres: eric: wal writer process                    postgres  5399  5393  0 18:25 ?        00:00:00 postgres: eric: autovacuum launcher process           postgres  5400  5393  0 18:25 ?        00:00:00 postgres: eric: stats collector process               root      5601  4945  0 18:39 pts/0    00:00:00 su - postgrespostgres  5602  5601  0 18:39 pts/0    00:00:00 -bashpostgres  5722     1  0 18:44 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /pgdata2postgres  5723  5722  0 18:44 ?        00:00:00 postgres: logger process               postgres  5725  5722  0 18:44 ?        00:00:00 postgres: checkpointer process         postgres  5726  5722  0 18:44 ?        00:00:00 postgres: writer process               postgres  5727  5722  0 18:44 ?        00:00:00 postgres: wal writer process           postgres  5728  5722  0 18:44 ?        00:00:00 postgres: autovacuum launcher process   postgres  5729  5722  0 18:44 ?        00:00:00 postgres: stats collector process      postgres  5730  5602  0 18:44 pts/0    00:00:00 ps -efpostgres  5731  5602  0 18:44 pts/0    00:00:00 grep postgres新的实例没有前缀cluster_name的标识,目前还是可以比较清楚的分辨2个实例,那如果有更多的实例在同1个OS中,就需要明确的区分了。设置新实例的cluster_name:修改postgresql.conf:cluster_name = 'gao'   重启新实例:-bash-3.2$ /usr/pgsql-9.5/bin/pg_c-D /pgdata2 restart -m fastpg_config       pg_controldata  pg_ctl          -bash-3.2$ /usr/pgsql-9.5/bin/pg_ctl -D /pgdata2 restart -m fast等待服务器进程关闭 .... 完成服务器进程已经关闭正在启动服务器进程-bash-3.2$ 日志:  日志输出重定向到日志收集进程提示:  后续的日志输出将出现在目录 "pg_log"中.不同的实例已经进行了区分:-bash-3.2$ ps -ef|grep postgrespostgres  5393     1  0 18:25 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /var/lib/pgsql/9.5/datapostgres  5394  5393  0 18:25 ?        00:00:00 postgres: eric: logger process                        postgres  5396  5393  0 18:25 ?        00:00:00 postgres: eric: checkpointer process                  postgres  5397  5393  0 18:25 ?        00:00:00 postgres: eric: writer process                        postgres  5398  5393  0 18:25 ?        00:00:00 postgres: eric: wal writer process                    postgres  5399  5393  0 18:25 ?        00:00:00 postgres: eric: autovacuum launcher process           postgres  5400  5393  0 18:25 ?        00:00:00 postgres: eric: stats collector process               root      5601  4945  0 18:39 pts/0    00:00:00 su - postgrespostgres  5602  5601  0 18:39 pts/0    00:00:00 -bashpostgres  5789     1  0 18:46 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /pgdata2postgres  5790  5789  0 18:46 ?        00:00:00 postgres: gao: logger process          postgres  5792  5789  0 18:46 ?        00:00:00 postgres: gao: checkpointer process    postgres  5793  5789  0 18:46 ?        00:00:00 postgres: gao: writer process          postgres  5794  5789  0 18:46 ?        00:00:00 postgres: gao: wal writer process      postgres  5795  5789  0 18:46 ?        00:00:00 postgres: gao: autovacuum launcher process   postgres  5796  5789  0 18:46 ?        00:00:00 postgres: gao: stats collector process   postgres  5797  5602  0 18:46 pts/0    00:00:00 ps -efpostgres  5798  5602  0 18:46 pts/0    00:00:00 grep postgres-bash-3.2$ /usr/pgsql-9.5/bin/pg_ctl -D /pgdata2 start正在启动服务器进程-bash-3.2$ 日志:  日志输出重定向到日志收集进程提示:  后续的日志输出将出现在目录 "pg_log"中.分别登录2个实例,验证一下可用性:-bash-3.2$ psql -p 5433psql (9.5beta2)输入 "help" 来获取帮助信息.postgres=# \l                                     数据库列表   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        -----------+----------+----------+-------------+-------------+----------------------- postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +           |          |          |             |             | postgres=CTc/postgres template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +           |          |          |             |             | postgres=CTc/postgres(3 行记录)postgres=# \q-bash-3.2$ psql -p 5432psql (9.5beta2)输入 "help" 来获取帮助信息.postgres=# \l                                     数据库列表   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        -----------+----------+----------+-------------+-------------+----------------------- music     | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/postgres         +           |          |          |             |             | postgres=CTc/postgres+           |          |          |             |             | eric=C*T*c*/postgres postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +           |          |          |             |             | postgres=CTc/postgres template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +           |          |          |             |             | postgres=CTc/postgres(4 行记录)OK~~~
08-29 02:05
查看更多