skip to Main Content

I am trying to install ApacheAGE and for this I am following the instruction in the following article but I have installed postgres 12 instead of 11. The installation was successful but when I tried to initialize DB using the command sudo ./bin/initdb demo I am getting the following 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.

How do I fix the issue? Do I need to create another Linux user for this?
I am using Ubuntu 22.04. Postgresql version is 12 and is installed in the following directory /usr/lib/postgresql/12/bin.

I created another Linux user, added that to sudoers list, logged in using su command but I am still getting the same error.

9

Answers


  1. The error code is telling you to not use sudo before the initdb command.

    Just do ./bin/initdb demo in that step.

    I’d advise to use commands with sudo only when it’s strictly necessary.

    Login or Signup to reply.
  2. Basically, if you prefix sudo with any Linux command, it will run that command with elevated privileges. Elevated privileges are required to perform specific administrative tasks. It is clear from your error message that initdb cannot be run as root. Also, it suggests you use an unprivileged user.

    So here ./bin/initdb <DbName> will to the work.

    Login or Signup to reply.
  3. If you’re still encountering the same error even after creating a new user and adding them to the sudoers list, it’s possible that you might be using sudo inadvertently while running the initdb command. Make sure you are not using sudo when executing the command.

    Follow these steps to ensure you are running the command as the correct user:

    1. Log in as the new user you created (let’s assume the username is
      myuser): sudo su – myuser
    2. Confirm that you are logged in as the correct user by running the
      whoami command: whoami

    This should return the username "myuser"

    1. Run the initdb command without using sudo: /usr/lib/postgresql/12/bin/initdb /path/to/your/data/directory

    Replace /path/to/your/data/directory with the actual path where you want to store the PostgreSQL data files.

    If you still encounter the error, there may be an issue with your PostgreSQL installation or the user permissions. In this case, try reinstalling PostgreSQL or consult the PostgreSQL documentation for further guidance.

    Login or Signup to reply.
  4. You don’t need to run the initdb command with privileges, try to run this without sudo

    Make sure that you have exported the PATH variables before run the make install.

    ./configure --prefix=/usr/local/pgsql-12
    sudo mkdir /usr/local/pgsql-12
    sudo chown [user] /usr/local/pgsql-12
    make install 
    
    Login or Signup to reply.
  5. simply do this, unprivileged command

    "./bin/initdb DBNAME"

    using sudo will give admin privileges

    Login or Signup to reply.
  6. You’re supposed to run the command as a non-root user, the convention is a user called "postgres".

    Login or Signup to reply.
  7. just add sudo before your command it will hopefully resolve your error.

    Login or Signup to reply.
  8. This error message indicates that you may have installed the Postgres version as a ROOT user and do not have the privilege to run it therefore.

    By right, you should be running the command as user postgres

    Try building and installing from source again after a clean uninstall. Please do not run as superuser during install.

    Login or Signup to reply.
    1. Set the PATH environment variable to include PostgreSQL’s bin directory.

    export PATH=/usr/local/pgsql/bin/:$PATH

    1. Set the PGDATA environment variable

    export PGDATA=/usr/local/pgsql/bin/data

    1. Create a data directory for PostgreSQL

    sudo mkdir -p /usr/local/pgsql/bin/data sudo chown $USER

    /usr/local/pgsql/bin/data

    1. Initialize the database cluster by running

    initdb

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search