Let's say I'm running pg_ctl init
or initdb
to initialise cluster.
When is the exact moment the default database inside this cluster is created?
- In the initdb procedure?
- After I run the server?
- After the first connection to the database?
CodePudding user response:
Creating a database cluster consists of creating the directories in which the database data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the
template1
andpostgres
databases.
(emphasis mine)
So it's created when you run initdb
CodePudding user response:
To see:
/usr/local/pgsql14/bin/initdb -D data/
cd data/
ls -al base/
drwx------ 2 postgres users 4096 Feb 9 08:17 1
drwx------ 2 postgres users 4096 Feb 9 08:17 13596
drwx------ 2 postgres users 4096 Feb 9 08:17 13597
ls -al base/1
total 8304
drwx------ 2 postgres users 4096 Feb 9 08:17 .
drwx------ 5 postgres users 4096 Feb 9 08:17 ..
-rw------- 1 postgres users 8192 Feb 9 08:17 112
-rw------- 1 postgres users 8192 Feb 9 08:17 113
-rw------- 1 postgres users 114688 Feb 9 08:17 1247
-rw------- 1 postgres users 24576 Feb 9 08:17 1247_fsm
-rw------- 1 postgres users 8192 Feb 9 08:17 1247_vm
...
From here [Database File Layout]:
base Subdirectory containing per-database subdirectories
So the 'system' databases are built upon using init(db)
.