0

I want to install postgres and psql via a Slackbuild and I did so. After installing it I tried running the command as suggested in the readme of the Slackbuild but I get an error upon running it as stated.

su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"

Should be one line, it looks like from what you copied and pasted you have su on one line and then the rest of the command on anther line. I'm not sure if that is just a formatting issue on your part, but it really doesn't look like that is the case. Also the error message you report seems to support the idea that your not giving the command on one line, and as such not running the command as the right user etc. I ran it on one file. I tried installing it via the source code and it was very complex so I went back to installing it via the Slackbuild. All is good until I need to run: Code:

su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"

This is what I get when I try to run the above command in multiple manners. It does not execute and I believe this is why Postgres is not working. I decided to ride this one out and learn how to use Slackware, Linux, PSQL and Full Stack so as to maximize my knowledge base and contribute to the Slackware project. I noticed while installing postgres via this blog that certain important yet archiac programs aren't in the Sbo website which I plan on adding. This guy has some interesting blogs on Slackware:http://www.pmoghadam.com/homepage/HT...esql-epkg.html Below is my attempts to execute the commands, what do you think is going on?

bash-5.1$ su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
Password: 
su: Authentication failure
bash-5.1$ sudo postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.
bash-5.1$ su - postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
Password: 
su: Authentication failure
bash-5.1$ su - postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
Password: 
su: Authentication failure
bash-5.1$ su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
Password: 
su: Authentication failure
bash-5.1$ su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
Password: 
su: Authentication failure
bash-5.1$ su postgres -c init -D /var/lib/pgsql/14/data --locale-en_US.UTF-8 -A md5 -W
su: invalid option -- 'D'
Usage: su [options] [-] [username [args]]

Options:
  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

If no username is given, assume root.



bash-5.1$ su
Password: 
bash-5.1# postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.
bash-5.1# su - postgres

We are upping our standards ... so up yours.
                -- Pat Paulsen for President, 1988

postgres@Bern:~$ postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"
2022-03-27 14:57:17.257 GMT [32152] FATAL:  unrecognized configuration parameter "initdb _D /var/lib/pgsql/14/data __locale"
postgres@Bern:~$ postgres -c initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W
2022-03-27 14:57:30.413 GMT [32155] FATAL:  -c initdb requires a value
postgres@Bern:~$

I don't know why this is not working given the fact I listened to and followed the holy read me. I trust in the Linux way and more importantly Slackware as a deity and magical piece of software. I will continue to search for results.

6.0 / import_db.sh

#!/usr/bin/env sh

dropdb sqlzoo
createdb sqlzoo
psql sqlzoo < data/create_tables.sql
ls

In the directory of a project file that needs to use psql. I receive the following errors:

bash-5.1# cd skeleton
bash-5.1# ls
Gemfile  Gemfile.lock  data  database  import_db.sh  lib  logfile  spec
bash-5.1# ./import_db.sh 
dropdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
Gemfile  Gemfile.lock  data  database  import_db.sh  lib  logfile  spec
bash-5.1# startdb
bash: startdb: command not found
bash-5.1# initdb
initdb: error: no data directory specified
You must identify the directory where the data for this database system
will reside.  Do this with either the invocation option -D or the
environment variable PGDATA.
bash-5.1# psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?
bash-5.1# 
bash-5.1# nano import_db.sh 

Why is this error occurring? Why can't I use psql?

4
  • use sudo instead of su: sudo -u postgres "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W" Commented Mar 27, 2022 at 15:12
  • Okay, now I have a new problem.
    – naarter
    Commented Mar 27, 2022 at 15:26
  • bash-5.1$ su Password: bash-5.1# sh /etc/rc.d/rc.postgresql start Starting PostgreSQL pg_ctl: could not open PID file "/var/lib/pgsql/14/data2/postmaster.pid": Permission denied This is what I get when I try to start the server locally.
    – naarter
    Commented Mar 27, 2022 at 15:54
  • If you have another problem ask here: unix.stackexchange.com/questions/ask Commented Mar 27, 2022 at 16:01

2 Answers 2

1

Okay, This should all be done by the SlackBuild and included with the package:

Code:

base database directory

assumes you are using /var/lib/pgsql as a homedir for postgres user

mkdir -p $PKG/var/lib/pgsql/$PG_VERSION/data chown -R postgres:postgres $PKG/var/lib/pgsql chmod 700 $PKG/var/lib/pgsql

permissions for DATADIR should be u=rwx (0700)

chmod 700 $PKG/var/lib/pgsql/$PG_VERSION/data

As long as OP runs the database creation command in the README.SBo as root, the subdirectories under /var/lib/pgsql/14/data should be the correct permissions as well.

Basically everything inside postgresql DATADIR should be owned by user postgres, the one that own the process. So chown LOCATION_OF_DATADIR should be sufficient. Code:

chown -R postgres:postgres /var/lib/pgsql

If you haven't created any postgresql user beside the default superuser which is the user postgres, then you should su to user postgres if you want to modify your database, including adding your own user to postgresql and after that you can login to postgresql using your own user instead of postgres.

$ su -
# su postgres
$ createuser your_username

Part of the linux security model works on the basis of principle of least privilege. Most processes run as a system user versus root. If you look at your /etc/passwd file there are many users besides root and your regular users, these are the system users. They only have enough permissions essential to run their service. The system user for postgresql is postgres. The database directories are owned and only have read/write privileges for user postgres.

Since system users do not usually have passwords you need to be logged in as root first then run su postgres -c initdb ...

or just from root

su postgres initdb ...

0

I came across same problem and this is what worked for me:

su -l

stop postgres in case its running

/etc/rc.d/rc.postgresql stop

then

cd /var/lib/pgsql/14/data

and finally issue command from Slackbuilds README.SBo

su postgres -c "initdb -D /var/lib/pgsql/14/data --locale=en_US.UTF-8 -A md5 -W"

after that i got prompt to enter database password and everything got configured.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .