"root" execution of the PostgreSQL server is not permitted

Your command does not do what you think it does. To run something as system user postgres:

 sudo -u postgres command

To run the command (also named postgres!):

sudo -u postgres postgres -D /usr/local/var/postgres

Your command does the opposite:

sudo postgres -D /usr/local/var/postgres

It runs the program postgres as the superuser root (sudo without -u switch), and Postgres does not allow to be run with superuser privileges for security reasons. Hence the error message.

If you are going to run a couple of commands as system user postgres, change the user with:

sudo -u postgres -i

... and exit when you are done.

  • PostgreSQL error: Fatal: role "username" does not exist

If you see this error message while operating as system user postgres, then something is wrong with permissions on the file or one of the containing directories.

postgres cannot access the server configuration file "/usr/local/var/postgres/postgresql.conf": Permission denied /usr/local/var/postgres/postgresql.conf

Consider instruction in the Postgres manual.
Also consider the wrapper pg_ctl - or pg_ctlcluster in Debian-based distributions.
And know the difference between su and sudo. Related:

  • PostgreSQL error: Fatal: role "username" does not exist

For those trying to run custom command using the official docker image, use the following command. docker-entrypoint.sh handles switching the user and handling other permissions.

docker-entrypoint.sh -c 'shared_buffers=256MB' -c 'max_connections=200'