【PostgreSQL】从零开始:(七)PostgreSQL-Initdb命令初始化数据库

概述

initdb是PostgreSQL初始化数据库的工具,用来生成PostgreSQL的Data目录结构

使用initdb 创建数据库集群时,会自动初始化区域设置支持。 initdb 默认情况下将使用其执行环境的区域设置来初始化数据库集群,因此,如果您的系统已设置为在数据库集群中使用您想要的区域设置,那么您不需要其他任何东西去做。如果您想使用不同的区域设置(或者您不确定系统设置为哪个区域设置),您可以通过指定 initdb 使用哪个区域设置4>选项。例如:--locale

命令

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
      --icu-locale=LOCALE   set ICU locale ID for new databases
      --icu-rules=RULES     set additional ICU collation rules for new databases
  -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
      --locale-provider={libc|icu}
                            set default locale provider for new databases
      --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:
  -c, --set NAME=VALUE      override default setting for server parameter
  -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 database files to disk, then exit

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/>
[postgres@postgre-sql ~]$ 

参数

参数中文解释
-A, --auth=METHOD

这个选项为本地用户指定在pg_hba.conf中使用的默认认证方法(hostlocal行)。initdb将使用指定的认证方法为非复制连接以及复制连接填充pg_hba.conf项。

为了安装方便,md5是默认值。

--auth-host=METHOD这个选项为通过 TCP/IP 连接的本地用户指定在pg_hba.conf中使用的认证方法(host行)。
--auth-local=METHOD这个选项为通过 Unix 域套接字连接的本地用户指定在pg_hba.conf中使用的认证方法(local行)。
-D, --pgdata=DATADIR这个选项指定数据库集簇应该存放的目录。这是initdb要求的唯一信息,但是你可以通过设定PGDATA环境变量来避免书写它,这很方便因为之后数据库服务器使用同一个变量来找到数据库目录。
-E, --encoding=ENCODING选择模板数据库的编码。这也将是后来创建的任何数据库的默认编码,除非你覆盖它。默认值来自于区域,或者如果该值不起作用则为SQL_ASCII
-g, --allow-group-access允许与集簇拥有者同组的用户读取initdb创建的所有集簇文件。 Windows会忽略此选项,因为它不支持POSIX样式的组权限。
--icu-locale=LOCALE设置ICU区域设置ID
--icu-rules=RULES设置其他ICU排序规则
-k, --data-checksum在数据页面上使用校验码来帮助检测 I/O 系统造成的损坏。启用校验码将会引起显著的性能损失。如果被设置,在所有数据库中会为所有对象计算校验码。 所有的检验故障均会在pg_stat_database视图中被报告。
--locale=LOCALE为数据库集簇设置默认区域。如果这个选项没有被指定,该区域将从initdb所运行的环境中继承。
--lc-collate=设置字符串排序顺序
--lc-ctype=设置字符分类(什么是字母?它的大写字母等效项?)
--lc-messages=LOCALE设置消息语言
--lc-monetary=设置货币金额的格式
-lc-numeric=设置数字格式
--lc-time=LOCALE设置日期和时间的格式
--no-locale等效于--locale=C
--locale-provider={libc|icu}设置默认区域设置提供程序libc或者icu
--pwfile=FILEinitdb从一个文件读取数据库超级用户的口令。该文件的第一行被当作口令。
-T, --text-search-config=CFG设置默认的文本搜索配置。但在任一数据库中使用相同的配置。--text-search-config=postgresql.conf
-U, --username=NAME选择数据库超级用户的用户名。这个的默认值是实际运行initdb的用户的名称。超级用户的名字是什么真的不重要,但是你可以选择保留常用的名字postgres,即使操作系统的用户名不同。
-W, --pwpromptinitdb提示要求为数据库超级用户给予一个口令。如果你没有计划使用口令认证,这就不重要。否则在你设置一个口令之前你就无法使用口令认证。
-X, --waldir=WALDIR这个选项指定预写式日志会被存储在哪个目录中。
--wal-segsize=SIZE

设置WAL段尺寸,以兆字节为单位。这是WAL日志中每个文件的尺寸。默认的尺寸为16兆字节。该值必须位于2的1次幂和1024次幂(兆字节)之间。这个选项只能在初始化期间设置,并且之后不能更改。

调整这个值来控制WAL日志传送或者归档可能会有用。此外,在有大量WAL的数据库中,每个目录中数量巨大的WAL文件可能会成为性能和管理问题。增加WAL文件尺寸将会降低WAL文件的数量。

-c, --set NAME=VALUE覆盖服务器参数的默认设置
-d, --debug生成调试输出
--discard-caches生成的调试输出不进行缓存,与-d, --debug配合使用
-L DIRECTORY指定输入文件(比如postgres.bki)位置
-n, --no-clean出现错误后不进行清理
-N, --no-sync默认情况下,initdb将等待所有文件被安全地写到磁盘之后再返回。这个选项会导致initdb不等待就返回,后续操作系统如果崩溃可能损坏数据目录。这个选项不适用于生产环境。
--no-instructions不要打印下一步的说明
-s, --show显示内部配置
-S, --sync-only安全地把所有数据库文件写入到磁盘并退出。这个指令不会执行任何数据库初始化操作。
-V, --version打印initdb版本并退出。
-?, --help显示有关initdb命令行参数的帮助并退出。

测试

[postgres@postgre-sql ~]$ initdb -U postgres -D /data/16.1/data2 --pwfile=<(printf "%s" "12345678ab")
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.

creating directory /data/16.1/data2 ... 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 ... ok

initdb: 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:

    pg_ctl -D /data/16.1/data2 -l logfile start

[postgres@postgre-sql ~]$ 

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DBA圈小圈

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值