I'm running a bash file from monit during boot, that bash file starts my postgres server.
if my database directory is not present, I do:
1- initdb (postgresql/data/)
su - edge -c '/usr/bin/initdb -D ${DBDIR}'
2- copy modified pg_hba.conf and postgresql.conf files to (postgresql/data/)
3- start my server
su - edge -c " /usr/bin/pg_ctl -w -D ${DBDIR} -l logfile start"
4- postgres createuser
- su - $User -c '${DBDIR} -e -s postgres'
after the execution of the bash file
postgresql/data/ is created
files are copied
server is started,
but user is not created so I cannot access my database
error : /usr/bin/psql -U postgres
psql: FATAL: role "postgres" does not exist
I can't decipher your step #4, but the reason why the postgres role does not exist is because the step #1 is run by a user edge and it doesn't ask for the creation of a postgres role through -U, so it creates an egde role as superuser instead.
Per initdb documentation:
-U username
--username=username
Selects the user name of the database superuser. This defaults to the name of the effective user running initdb. It is really not
important what the superuser's name is, but one might choose to keep
the customary name postgres, even if the operating system user's name
is different.
Either do initdb -U postgres, or if you prefer a superuser named edge, keep it like this but start psql with psql -U edge, or set the PGUSER environment variable to edge to avoid typing that each time.
Related
i'm trying to see if i can run commands after "entering" into a container:
#!/bin/sh
# 1 - create a new user in the db for mautic
podman exec -it postal-mariadb mysql -h 127.0.0.1 -u root -p$1 <<EOF
CREATE DATABASE mauticdb;
CREATE USER 'mautic' IDENTIFIED BY /'$1/';
GRANT ALL PRIVILEGES ON mauticdb.* TO 'mautic';
FLUSH PRIVILEGES;
exit
EOF
this gives me an error: Error: container create failed (no logs from conmon): EOF
but im thinking maybe this is not a good use of HERE DOCS
something like this doesn't work either:
echo $1 | podman exec -it postal-mariadb mysql -h 127.0.0.1 -u root -p postal-server-1 -e 'select * from deliveries limit 10;'
That's a fine (and common) use of here docs, although you probably want to drop the -t from your podman command line. If I have a mariadb container running:
podman run -d --name mariadb -e MARIADB_ROOT_PASSWORD=secret docker.io/mariadb:10
Then if I put your shell script into a file named createdb.sh, modified to look like this for my environment:
podman exec -i mariadb mysql -u root -p$1 <<EOF
CREATE DATABASE mauticdb;
CREATE USER 'mautic' IDENTIFIED BY '$1';
GRANT ALL PRIVILEGES ON mauticdb.* TO 'mautic';
FLUSH PRIVILEGES;
EOF
I've made three changes:
I've removed the -t from the podman exec command line, since we're passing input on stdin rather than starting an interactive terminal;
I removed the unnecessary exit command (the interactive mysql shell will exit when it reaches end-of-file);
I removed the weird forward slashes around your quotes (/'$1/' -> '$1').
I can run it like this:
sh createdb.sh secret
And it runs without errors. The database exists:
$ podman exec mariadb mysql -u root -psecret -e 'show databases'
Database
information_schema
mauticdb <--- THERE IT IS
mysql
performance_schema
sys
And the user exists:
$ podman exec mariadb mysql -u root -psecret mysql -e 'select user from user where user="mautic"'
User
mautic
I have seen all the questions here on stackoverflow about similar topic, but I would like to know why this happens, where is the problem, and what to do to set it up correctly. I am learning to code, so I apologize for any misunderstandings. Please, have patience with me.
My case is -
I cannot run psql command from terminal.
Respond is -
psql: FATAL: role "some_name" does not exist
after I write down and hit enter -
sudo -u postgres -i
everything just works and I can run psql command. I need to write sudo -u postgres -i command every time I open terminal, again and again.
I would like to kindly thanks to any respond.
If you got more questions I can give you more information.
Here see some additional info:
[postgres#localhost ~]$ psql
psql (9.6.6)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member
of
-----------+------------------------------------------------------------+-------
----
matus | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
Another:
[postgres#localhost ~]$ whoami
postgres
So, I have two opened terminals, one as postgres and another as user_name where I can do other stuff, using db somehow.
from your description I suppose you have peer authentication in hba file. To check it connect to db with psql (your sudo -u postgres method) and:
t=# show hba_file ;
hba_file
---------------------
/pg/d10/pg_hba.conf
(1 row)
t=# \! grep local /pg/d10/pg_hba.conf | grep -v "#"
local all all peer
if you see peer above trust, or just peer, than this is the case. from manuals:
https://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-PEER
The peer authentication method works by obtaining the client's
operating system user name from the kernel and using it as the allowed
database user name (with optional user name mapping)
formatting mine.
so in order to avoid
psql: FATAL: role "some_name" does not exist
just create such db user:
create user some_name;
or change peer to trust in hba.conf
I'm new to Postgres and Bash so I'm not sure what the difference is.
I'm trying to automate in a bash script updating a table in Postgres. I have the .sql file and I've created .pgpass file with 600.
The a script that is provided to me uses sudo -u postgres psql db -w < .sql and it fails because it can't find the pass.
Whereas, sudo psql -U postgres db -w < .sql doesn't prompt for a pass and is able to update.
So what's the difference? Why can't the first command get the pass from the .pgpass?
sudo -u postgres is running the rest of the command string as the UNIX user postgres
sudo psql -U postgres db -w is running the command as the UNIX user root and (presumeably) connecting to postgres as the user "postgres"
Probably the .pgpass file doesn't exist for the unix user postgres.
It is a case of peer autentication. If you're running user x and you have user x on your database you're trusted by postgres so you don't have to use password (default settings of instalation). Running sudo psql -u x you're trying to connect from user root to database as user x... root!=x so you need password. Client authentication is controlled by a configuration file pg_hba.conf You can also provide password via .pgpass file. You'll find all needed informations in PostgreSQL documentation.
Recently updated from Postgresql 9.1 to 9.3.
Everything works fine, but I noticed now when I type in:
sudo -u postgres psql
I am getting hit with a permission denied error for changing dir to root.
"Could not change directory to /home/root.
However, when I use:
sudo su - postgres
psql
It accesses it fine. How can I fix this?
change directory to someplace that postgres has access to:
cd /tmp
sudo -u postgres psql
Try this:
sudo -i -u postgres psql
This accomplishes (almost) the same thing as your
sudo su - postgres
The - in the above indicates that you want to use the postgres account's environment. If you remove the -, it will fail similarly to sudo -u
The -i indicates that you want to run the postgres account's login shell (hence cding to their home directory).
For me this did the trick (or you'll get a could not change directory to "/root": Permission denied), pay attention to quotes (')
sudo -Hiu postgres 'pg_dump --column-inserts --data-only --table=someTable entities_db > /var/backups/anywhere/$(date +%Y%m%d_%H%M%S)_someTable.sql'
Note the -Hiufor sudo, or use su - postgres
you can also put that in a cronjob for root with crontab -e
I'm scripting a system setup and already have postgres installed. Here is a test script (run as root) to try and report the working directory in postgres. Calling pwd as postgres gives /var/lib/postgresql. But the test..
#!/bin/bash
su - postgres
pwd > /home/me/postgres_report
exit
.. fails (obviously) and reports the original working directory. And afterwards the bash shell is stuck in postgres, suggesting the commands are not being called in right order. I understand the bash environmental issues here. I don't have a clue how to do what I need to do, which is automate a postgres process that I can easily do interactively (i.e. step into postgres, execute a command, and exit). Any pointers?
Use sudo.
Use one of:
Passing a one line command to psql
sudo -u postgres psql -c "SELECT ..."`
A here document:
sudo -u postgres psql <<"__END__"
SELECT ...;
SELECT ...;
__END__
(If you want to be able to substitute in shell variables leave out the ", e.g. <<__END__, and backslash escape $ signs you don't want to be variables)
Pass a file to psql
sudo -u postgres psql -f /path/to/file
The sudo -u postgres is of course only required if you need to become the postgres system user to run tasks as the postgres database user via peer authentication. Otherwise you can use psql -U username, a .pgpass file, etc.
#!/bin/bash
# run as root
[ "$USER" = "root" ] || exec sudo "$0" "$#"
echo "=== $BASH_SOURCE on $(hostname -f) at $(date)" >&2
sudo passwd postgres
echo start the postgres
sudo /etc/init.d/postgresql start
sudo su - postgres -c \
"psql <<__END__
SELECT 'crate the same user' ;
CREATE USER $USER ;
ALTER USER $USER CREATEDB;
SELECT 'grant him the priviledges' ;
grant all privileges on database postgres to $USER ;
alter user postgres password 'secret';
SELECT 'AND VERIFY' ;
select * from information_schema.role_table_grants
where grantee='""$USER""' ;
SELECT 'INSTALL EXTENSIONS' ;
CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";
CREATE EXTENSION IF NOT EXISTS \"pgcrypto\";
CREATE EXTENSION IF NOT EXISTS \"dblink\";
__END__
"
sudo /etc/init.d/postgresql status
sudo netstat -tulntp | grep -i postgres