A) Mounted a Virtualbox directory with the required ownership and permissions:

ls-l /var:

drwx------.  1 postgres postgres 4096 Aug 20 12:43 pgshared

(and /var/pgshared/data directory has been created with exactly same permission)

B) Changed PGDATA environment variable from default to this data directory

C) initdb failed with below error:

fixing permissions on existing directory /var/pgshared/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 ... Europe/Brussels
creating configuration files ... ok
running bootstrap script ... 2021-08-20 12:16:08.088 CEST [2807] LOG:  could not link file "pg_wal/xlogtemp.2807" to "pg_wal/000000010000000000000001": Operation not permitted
2021-08-20 12:16:08.090 CEST [2807] FATAL:  could not open file "pg_wal/000000010000000000000001": No such file or directory
child process exited with exit code 1

initdb: removing contents of data directory "/var/pgshared/data"

Additional comments:

Using -D option has the same result (regardless of PGDATA environment variable)

2

There are 2 best solutions below

0
On

The error means that the file system does not support hard links (the link(2) system call fails).

Even if you manage to put a data directory, PostgreSQL will not work. To confirm, run the following a couple of times:

SELECT pg_switch_xlog();

You will have to use a different file system that implements all required system calls.

0
On

This is a workaround that I used and I was able to successfully continue with creating databases and connecting to them but I don't have any idea (yet) if I'll face further issues. Additionally, the root cause and a real solution is still unknown:

1- Since the initialization could be done on a local drive, I unmounted the shared drive (umount /var/pgshared) and initialized the database. it was successful and the data folder got created with all expected contents.

2- I moved the data folder to a temporary location and re-mounted the shared folder with the same mount command.

3- I moved back the data folder to the mounted drive.

4- I made simple test with psql commands.