I need to use root user to run scripts at crontab, for example to read and write on all /home folders.
But something that I need to do also in the shell script is to run psql. Problem:
my user (me = whoami and not is root) can run for example psql -c "\l"
the root user not works (!) with psql -c "\l"... And error not make sense "psql: error: could not connect to server: FATAL: database "root" does not exist".
How to enable root to run psql?
PS: looking for a kind of "GRANT ALL PRIVILEGES ON ALL DATABASES TO root".
root is allowed to run psql, but nobody can connect to a database that doesn't exist.
The default value for the database user name with psql is the operating system user name, and the default for the database is the same as the database user name.
So you have to specify the correct database and database user explicitly:
psql -U postgres -d postgres -l
The next thing you are going to complain about is that peer authentication was denied.
To avoid that, either run as operating system user postgres or change the rules in pg_hba.conf.
Related
I want to export table from ma database using remote access (ssh) and psql (\copy command) but failed
to resume:
I have a database named mydatabase and user named myuser granted on this database
table I try to extract is named mytable
I connect to my remote server using Putty
once connected, I run psql : sudo -u postgres psql (I tried to connect using myuser but failed because mysuser is unknown ???)
I connect to my database : \c mybase
I run \copy mytable TO '/home/path/to/my/file/file.txt'
and get error message: '/home/path/to/my/file/file.txt' : Permission denied
as I said, I tried to connect using myuser thinking it could solve permission issue but don't know why it failed...
psql -d mydatabase -U myuser
psql: error: could not connect to server: FATAL: Peer authentication failed for user "myuser"
Let's assume the operating system user you connect with using Putty is x.
Since you run psql as user postgres using the sudo command, it is the operating system user postgres that needs permissions to write /home/path/to/my/file/file.txt.
So user x must give postgres the required permissions.
I'm running MariaDB MySQL on a Linux machine. When I attempt to change the password following the documentation, MySQL gives me the following error upon starting:
2017-11-17 9:25:47 139640910462912 [Note] Server socket created on IP: '::'.
ERROR: 1064 You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near
'USER 'root'#'localhost' IDENTIFIED BY 'pA$sWord^';' at line 1
2017-11-17 9:25:47 139640910462912 [Note] mysqld: ready for connections.
Version: '10.1.28-MariaDB' socket: '/run/mysqld/mysqld.sock' port: 3306 MariaDB Server
The specific documentation I'm following:
The instructions assume that you will start the MySQL server from the
Unix login account that you normally use for running it. For example,
if you run the server using the mysql login account, you should log in
as mysql before using the instructions. Alternatively, you can log in
as root, but in this case you must start mysqld with the --user=mysql
option. If you start the server as root without using --user=mysql,
the server may create root-owned files in the data directory, such as
log files, and these may cause permission-related problems for future
server startups. If that happens, you will need to either change the
ownership of the files to mysql or remove them.
Log on to your system as the Unix user that the MySQL server runs as
(for example, mysql).
Stop the MySQL server if it is running. Locate the .pid file that
contains the server's process ID. The exact location and name of this
file depend on your distribution, host name, and configuration. Common
locations are /var/lib/mysql/, /var/run/mysqld/, and
/usr/local/mysql/data/. Generally, the file name has an extension of
.pid and begins with either mysqld or your system's host name.
Stop the MySQL server by sending a normal kill (not kill -9) to the
mysqld process. Use the actual path name of the .pid file in the
following command:
shell> kill `cat /mysql-data-directory/host_name.pid`
Use backticks (not forward quotation marks) with the cat command. These cause the
output of cat to be substituted into the kill command.
NOTE: I used top to end the process. I'm certain it is not running.
$ ps aux | grep mysql
user 30201 0.0 0.0 10884 2296 pts/6 S+ 09:43 0:00 grep mysql
Create a text file containing the password-assignment statement on a
single line. Replace the password with the password that you want to
use.
MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Save the
file. This example assumes that you name the file /home/me/mysql-init.
The file contains the password, so do not save it where it can be read
by other users. If you are not logged in as mysql (the user the server
runs as), make sure that the file has permissions that permit mysql to
read it.
Start the MySQL server with the special --init-file option:
shell> mysqld --init-file=/home/me/mysql-init &
The server executes
the contents of the file named by the --init-file option at startup,
changing the 'root'#'localhost' account password.
Other options may be necessary as well, depending on how you normally
start your server. For example, --defaults-file may be needed before
--init-file.
After the server has started successfully, delete /home/me/mysql-init.
MariaDB Version:
$ sudo pacman -Q | grep mariadb
libmariadbclient 10.1.28-1
mariadb 10.1.28-1
mariadb-clients 10.1.28-1
File /change_root_password.txt:
$ ls -lha /change_root_pwd.txt
-rwxrwxrwx 1 mysql mysql 56 Nov 17 09:24 /change_root_pwd.txt
$ cat /change_root_pwd.txt
ALTER USER 'root'#'localhost' IDENTIFIED BY 'pA$sWord^';
Command used to start mysqld that results in the above mentioned error:
sudo mysqld --user=mysql --init-file=/change_root_pwd.txt
I found this documentation with an alternate SQL query:
If the ALTER USER statement fails to reset the password, try repeating
the procedure using the following statements to modify the user table
directly:
UPDATE mysql.user
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
When I try that instead of the ALTER USER query, I get no error, but the password does not get updated.
Perhaps I should have understood the difference between MariaDB and MySQL before asking this question.
I do not know why using that method to reset the root Password does not work, but MariaDB has a simpler way to change the root Password which is documented here:
https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7
Reset the MariaDB Root Password
If you forget your root MariaDB password, it can be reset.
Stop the current MariaDB server instance, then restart it with an option to not ask for a password:
sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables &
Reconnect to the MariaDB server with the MariaDB root account:
mysql -u root
Use the following commands to reset root’s password. Replace password with a strong password:
use mysql;
update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
flush privileges;
exit
Then restart MariaDB:
sudo systemctl start mariadb
I previously asked how to make a backup of a Firebird database in
I need to backup or clone one remote firebird database or export it to Sql server
Now the backup is complete, but when I try to restore it to Firebird on my computer, I get an error.
I use this command:
gbak -r -p 4096 -o e:\mybackup.fbk localhost:e:\bddados.fdb -user sysdba -pas masterkey
The error I receive is
gbak: ERROR:Your user name and password are not defined. Ask your database administrator to set up a Firebird login. gbak:Exiting before completion due to errors
But I test my Firebird locally with this user and password and it's ok. Does the created backup database need to specify in generate command a password or do I need to use the same of the old database?
user and pas[sword] parameters should be before the path to files
gbak -r -p 4096 -o -user sysdba -pas masterkey e:\mybackup.fbk localhost:e:\bddados.fdb
gbak documentation
I dont have a lot of experience with Postgres, so I'm having a bit of a trouble accessing a database I just rescued from a broken Ubuntu server.
What I'm trying to do: The server that was sporting postgres is dead now, I can only access it via "rescue mod" provided by the hosting company. I tried chroot in order to dump the database using pg_dump or pg_dumpall, but it seems that the server is unreachable in this way. The dump attempts to enroute himself via rescue.ovh.net (OVH being the hosting provider), even if I specify -h localhost.
So I came with a different idea: copying the whole Postgres folder into a local machine, and then dumping the database and keep on restoring everything starting from this dump. This is something you can do in MySQL, so I thought that this maybe would be possible with Postgres.
But so far it is not workin. I copied /var/lib/postgres folder to my local machine (taking care that the owner in my local machine is also the postgres user), but when I try to dump a database, I can't really do it.
The errors vary on the command:
My first attempt is to dump the database using the database user:
$ pg_dump -U discourse -h localhost > discourse_prod.sql
Password:
pg_dump: [archiver (db)] connection to database "discourse" failed: fe_sendauth: no password supplied
The prompt asks for a password, the user did not have a password, but If i just press enter it aches and says no password provided.
My second attempt was dumping via postgres admin user:
sudo pg_dump -U postgres discourse_prod > ~/test.sql
I get a pg_dump: [archiver (db)] connection to database "discourse_prod" failed: FATAL: Peer authentication failed for user "postgres". So I try to switch user before trying to dump...
sudo -u postgres pg_dump -Fp discourse_prod > dump.sql
and now it seems that the database was not properly copied: pg_dump: [archiver (db)] connection to database "discourse_prod" failed: FATAL: database "discourse_prod" does not exist
As I said, I have not very much experience with Postgres, and I'm running out of ideas... on how to get a dump out from these files, i don't mind if I manage to get it from the devastated machine or from my locally copied files.
Any ideas?
I have a shell script which runs on deployment and I have these lines:
# Database
createdb $DBNAME
createuser -D -A $DBNAME
However, in my logs I get this error:
createdb: could not connect to database postgres: FATAL: Ident
authentication failed for user "root"
createuser: could not connect to database postgres: FATAL: Ident authentication failed for
user "root"
Would anyone mind telling me what is going wrong here and how I can correct my lines. Surely root should have permission to do this?
the best way to do this, especially if you want your script to be portable, is:
su --login postgres --command "createdb $DBNAME"
this should be safer, more secure, and more portable than using -U. i do it this way in all my posgreSQL scripts. you might find it a useful technique. obviously it still needs to be run as a root user (e.g. with sudo).
If your DB is secured, you need to connect as a DB user, not as a user of the OS. For example:
createdb -U dbrootuser -W $DBNAME
See this link for full syntax