0

When running bin/initdb demo I encounter the following error:

The files belonging to this database system will be owned by user "USER_1".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  en_US.UTF-8
  CTYPE:    en_US.UTF-8
  MESSAGES: en_US.UTF-8
  MONETARY: ur_PK
  NUMERIC:  ur_PK
  TIME:     ur_PK
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory newDB ... initdb: error: could not create directory "newDB": Permission denied

Running it as root gives me this error:

initdb: error: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

What could be the problem here, would appreciate any help.

ShaHeen
  • 59
  • 5
  • The docs are there for a reason [initdb](https://www.postgresql.org/docs/current/app-initdb.html): *initdb must be run as the user that will own the server process, because the server needs to have access to the files and directories that initdb creates. Since the server cannot be run as root, you must not run initdb as root either. (It will in fact refuse to do so.)* – Adrian Klaver Jul 03 '23 at 18:46
  • **Add to question the following information** 1) What OS are you doing this on? 2) How did you install Postgres? – Adrian Klaver Jul 03 '23 at 18:52
  • I am doing it in ubuntu and install it through terminal. – ShaHeen Jul 04 '23 at 06:25
  • Are you using the Ubunru packages? If so then `sudo pg_createcluster` will work. If not you should install the packages. As it happens I just posted two blog posts about this [Install packages](https://aklaver.org/wordpress/2023/06/29/postgres-debian-ubuntu-packagingpart-1/) and [Package commands](https://aklaver.org/wordpress/2023/07/01/postgres-debian-ubuntu-packagingpart-2/). – Adrian Klaver Jul 04 '23 at 14:50

4 Answers4

1

You cannot run initdb with sudo.

The steps prior to initdb should be like so:

sudo mkdir /usr/local/pgsql-12
sudo chown [user] /usr/local/pgsql-12
make install
export PATH=/usr/local/pgsql-12/bin/:$PATH
export PGDATA=/usr/local/pgsql-12/bin/data

then, make sure you create the main cluster first before creating others

initdb

then, you can create others with a name assigned

initdb [db name]

This should solve that.

Tito
  • 289
  • 8
0

You dont need to run it with sudo. But you need to get permissions before you use initdb. Use :

sudo chown <username> /path/to/postgres/bin/

usually the path is something like

/usr/local/pgsql/bin/

You can find by typing pg_config. The correct path is in the variable BINDIR.

  • This is not a valid answer. You do not need to change the ownership on the binaries. You just need to run the `initdb` as the user that owns the `$DATA` directory. Per [initdb](https://www.postgresql.org/docs/current/app-initdb.html) that cannot be `root`. – Adrian Klaver Jul 03 '23 at 18:51
  • i encountered the same issue and I solved it by running that command thats why I proposed that – Panagiotis Foliadis Jul 03 '23 at 19:16
  • 1) Yes but you have now changed the binaries to be run by only one user, which means every time you use one of them you will need to be that user. That is a) counter productive and b) not necessary. 2) The issue was running `initdb` as `root` which is explicitly forbidden and the answer does not address that. – Adrian Klaver Jul 03 '23 at 19:32
  • ok thank you for the info! – Panagiotis Foliadis Jul 03 '23 at 19:42
0

If you have installed your PostgreSQL correctly, the following commands should solve this:

bin/initdb demo
pg_ctl -D /usr/local/pgsql-12/bin/data -l logfile start
bin/createdb demo
bin/psql demo

if it's doesn't work, I suggest you to follow this Tutorial to how to install Postgresql and AGE

Marcos Silva
  • 115
  • 5
0

Simply change the pgsql folder ownership (pgsql represents the folder you created to install a postgres version), by running the following command:

sudo chown new_owner /usr/local/pgsql

Note: change the path /usr/local/pgsql to postgres folder path on your system.

another note, whenever later you encounter similar issues of permission denied, it almost always be solved using either sudo before running the command, or by changing the ownership (permissions) to a certain folder.

I hope this helps you

ahmed_131313
  • 142
  • 6