initdb official website
initdb creates a new PostgreSQL database cluster. A database cluster is a collection of databases managed by a single server instance.
Creating a database cluster includes the directory where the database resides, generating shared directory tables (tables belonging to the entire cluster rather than any specific database), and creating template1 and postgres databases. When a new database is created later, everything in the template1 database is copied (so anything installed in template1 is automatically copied to each database created later.), the postgres database is the default database for users, instance programs, and third-party applications.
Although initdb will try to create the specified data directory, if the parent directory of the required data directory is the root directory, it may not have permissions. To initialize in such a setting, you need to create an empty data directory as the root directory, then use chown to assign ownership of the directory to the database user account, and then su becomes the database user running initdb.
initdb must be run as a user with the server process, because the server needs to access the files and directories created by initdb. Since the server cannot run as root, initdb cannot be run as root.
For security reasons, by default, new clusters created by initdb can only be accessed by the cluster owner. —The allow-group-access option allows any user who belongs to the same group as the cluster owner to read files in the cluster. This is very useful for performing backups as non-privileged users.
initdb initializes the default locale and character set encoding of the database cluster. When creating a database, you can set character set encoding, sorting order (LC_COLLATE) and character set classes (LC_CTYPE, such as upper, lower, and digit). initdb determines those settings for template1 database, which will serve as the default settings for all other databases.
To change the default sort order or character set class, use the --lc-collate and --lc-ctype options. Sorting orders other than C or POSIX can also cause performance losses. For these reasons, it is important to choose the correct locale when running initdb.
The remaining locale categories can be changed after the server starts. You can also use -locale to set default values for all locale categories, including sorting order and character set classes. All server locale values (lc_*) can be displayed through SHOW All.
Common parameters:
-D directory/--pgdata=directory
This option specifies the directory where the database cluster should be stored. Must pass. The environment variable PGDATA can also be set to replace the -D option.
-U username/--username=username
Select the username of the database superuser. This defaults to the name of the user running initdb.
-E encoding/--encoding=encoding
Select the encoding of the template database. This is also the default encoding for any database you create later,
--lc-collate/--lc-ctype
Change the default sort order or character set class.
-k/--data-checksums
Use checksums on the data page to help detect corruption of the I/O system, otherwise the system will be silent. Enabling checksums can result in significant performance losses. If set, the checksum of all objects in all databases is calculated. All checksum failures will be reported in the pg_stat_database view.
So the possible commands used to initialize postgresql are:
su - pguser001 -c "/u01/pgsql/bin/initdb --username=pguser001 --encoding=UTF8 --lc-collate=C --lc-ctype=en_US. utf8 --data-checksums -D /data"
Note: If the data directory already exists and is initialized, initdb will not run.
Supplement: Postgresql initialization initdb No such file or directory appears
Appear
FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/file_fdw.control": No such file or directory.
as follows:
me@me:/opt/HighGo/Develop/bin$ <span style="color:rgb(51,51,255);">./initdb -D ../data -W</span>
The files belonging to this database system will be owned by user "me". 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". creating directory ../data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 32MB creating configuration files ... ok creating template1 database in ../data/base/1 ... ok initializing pg_authid ... ok Enter new systemuser password: Enter it again: Enter syssao password: Enter it again: Enter syssso password: Enter it again: setting dba password ... ok initializing dependencies ... ok <span style="color:rgb(204,0,0);">creating system views ... FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/file_fdw.control": No such file or directory STATEMENT: /*</span> * PostgreSQL System Views * * Copyright (c) 1996-2012, PostgreSQL Global Development Group * * src/backend/catalog/system_views.sql
The reason is that the file_fdw.control plugin is not installed. Enter the contrib directory in the source code directory. Find the file_fdw file and enter it, compile and install it,
make make install
Similarly, it appears:
creating system views ... FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/": No such file or directory.
Enter the dblink file in the contrib directory, make, make install
So if some other things appear, etc., the corresponding installation will be carried out.
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.