PostgreSQL服务端程序之initdb
1.简介
initdb位于$PGHOME目录下,属于服务器端程序,官方文档描述为"创建一个新的PostgreSQL数据库集簇",通俗来讲就是创建数据文件目录。该命令必须用root账号运行,详细用法如下:
[pg14@test bin]$ ll $PGHOME/bin/initdb
-rwxr-xr-x 1 root root 147952 Nov 1 19:31 /u01/app/pg14/bin/initdb
[pg14@test bin]$ initdb --help
initdb initializes a PostgreSQL database cluster.
Usage:
initdb [OPTION]... [DATADIR]
Options:
-A, --auth=METHOD default authentication method for local connections
--auth-host=METHOD default authentication method for local TCP/IP connections
--auth-local=METHOD default authentication method for local-socket connections
[-D, --pgdata=]DATADIR location for this database cluster
-E, --encoding=ENCODING set default encoding for new databases
-g, --allow-group-access allow group read/execute on data directory
-k, --data-checksums use data page checksums
--locale=LOCALE set default locale for new databases
--lc-collate=, --lc-ctype=, --lc-messages=LOCALE
--lc-monetary=, --lc-numeric=, --lc-time=LOCALE
set default locale in the respective category for
new databases (default taken from environment)
--no-locale equivalent to --locale=C
--pwfile=FILE read password for the new superuser from file
-T, --text-search-config=CFG
default text search configuration
-U, --username=NAME database superuser name
-W, --pwprompt prompt for a password for the new superuser
-X, --waldir=WALDIR location for the write-ahead log directory
--wal-segsize=SIZE size of WAL segments, in megabytes
Less commonly used options:
-d, --debug generate lots of debugging output
--discard-caches set debug_discard_caches=1
-L DIRECTORY where to find the input files
-n, --no-clean do not clean up after errors
-N, --no-sync do not wait for changes to be written safely to disk
--no-instructions do not print instructions for next steps
-s, --show show internal settings
-S, --sync-only only sync data directory
Other options:
-V, --version output version information, then exit
-?, --help show this help, then exit
If the data directory is not specified, the environment variable PGDATA
is used.
Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
2.重要选项
-A:这个选项用于指定在pg_hba.conf中使用的默认认证方法,可以不用指定,采用默认方法,数据库实例启动后根据需求单独配置pg_hba.conf文件。具体认证方法可以参考pg_hba.conf文件的介绍部分。
-D | --pg_data: 指定数据库数据文件的存放路径,为必须指定选项。可以设定环境变量$PGDATA来替代完整的目录,这样也方便后续数据库实例启动时指定。如果指定为目录不存在,initdb程序会尝试创建,如果对上一级目录没有权限,就会报错,因此为了安全起见,建议先创建好数据文件目录,赋予数据库用户和组合适的权限,再初始化数据库集蔟。
-E | --econding :指定模板数据库(template0/template1)的编码,后续创建的任何数据库都使用这样的编码。
-k | --data-checksum :在数据页面使用校验码来检测IO坏块,启用后,可以在pg_stat_database视图中查看错误信息,启用后会降低数据库性能。
–pwfile :从一个文件中读取数据库超级用户的密码,第一行被当做数据库超级用户密码
-T:–text-search-config :设置默认的文本搜索配置
-U| --username :选择数据库超级用户的用户名。这个的默认值是实际运行initdb
的用户的名称。
-W|-pwprompt :设置超级用户的口令。
-X |–waldir: 设置WAL日志的存放路径
-wal-segsize:设置置WAL段尺寸,以兆字节为单位。这是WAL日志中每个文件的尺寸。默认的尺寸为16兆字节。这个选项只能在初始化期间设置,并且之后不能更改。调整这个值来控制WAL日志传送或者归档可能会有用。此外,在有大量WAL的数据库中,每个目录中数量巨大的WAL文件可能会成为性能和管理问题。增加WAL文件尺寸将会降低WAL文件的数量。
3.执行测试
[root@test data]# mkdir pgdata
[root@test data]# chown pg14:pg14 pgdata
[pg14@test ~]$ initdb -D /data/pgdata-k -U postgres -W
The files belonging to this database system will be owned by user "pg14".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
Enter new superuser password:
[pg14@test ~]$ initdb -D /data/pgdata -k -U postgres -W
The files belonging to this database system will be owned by user "pg14".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are enabled.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /data/pgdata ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
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:
pg_ctl -D /data/pgdata -l logfile start