莱莱 发表于 2024-8-9 15:34:53

PostgreSQL自带的下令行工具21- initdb

PostgreSQL自带的下令行工具21- initdb

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
initdb 是 PostgreSQL 中的一个工具,用于创建一个新的 PostgreSQL 数据库集群。数据库集群是一组数据库和一些与这些数据库相干的初始化信息,包括设置文件(如 postgresql.conf 和 pg_hba.conf)以及事件日记等。实际上,initdb 主要负责初始化数据库体系的文件体系情况。
通过help检察帮助文档。
$ initdb --help
initdb initializes a PostgreSQL database cluster.

Usage:
initdb ...

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-accessallow 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/>
基本使用

initdb 的基本用法涉及指定要创建新数据库集群的目录。以下是一个简朴的下令行示例:
initdb -D /path/to/data_directory
这里的 -D /path/to/data_directory 指定了新数据库集群的数据目录。如果不使用 -D 选项,则 initdb 会使用情况变量 PGDATA 指定的目录,如果 PGDATA 也没有设置,initdb 就会报错。
如之前的初始化过程
$ initdb -D /home/pg16/data -U postgres -k
The files belonging to this database system will be owned by user "pg16".
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.

creating directory /home/pg16/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 ... America/Los_Angeles
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 /home/pg16/data -l logfile start
主要选项



[*]-D – 指定数据库集群的数据目录。
[*]-E – 设置默认字符编码方式。例如,使用 -E UTF8 来设置 UTF-8 编码。
[*]--locale – 用于设置数据库的区域设置,这将影响字符的排序规则和种别行为。
[*]--lc-collate, --lc-ctype, --lc-messages, --lc-monetary, --lc-numeric, --lc-time – 这些选项允许你单独设置数据库在差异方面的区域设置,如排序规则、字符分类、消息语言、货币格式、数字格式和日期时间格式。
留意事项



[*]运行 initdb:只有在首次设置 PostgreSQL 安装时才必要手动运行 initdb。大多数时候,当你通过包管理器安装 PostgreSQL 时(例如 apt、yum、brew 等),这一步调会主动为你完成。
[*]权限:initdb 必须以 PostgreSQL 服务器将要运行的用户身份来执行,通常这个用户称为 postgres。如果以其他用户身份运行,那么数据库服务器可能无法访问 initdb 创建的文件和目录。
[*]安全性:在初始化数据库时思量安全设置非常紧张。特殊是,设置文件 pg_hba.conf 控制客户端认证和连接,应该在初始化之后进行相应的调整以确保数据库的访问安全。
[*]之后操作:一旦使用 initdb 初始化了数据目录,你就可以启动数据库服务器了,通常使用 pg_ctl start 或直接运行 postgres 可执行文件,具体取决于你的体系设置和偏好。
initdb 是 PostgreSQL 安装和设置过程中的关键步调,正确使用它对于确保数据库集群正确、安全地运行至关紧张。
服膺:心存敬畏,行有所止。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: PostgreSQL自带的下令行工具21- initdb