posgresql/psql: '/home/path/to/my/file/file.txt' : Permission denied - ssh

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.

Related

Root with no permission to work with psql

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.

cannot connect to postgres :psql: error: could not connect to server: FATAL: password authentication failed for user on windows

I installed PostgreSQL on my windows 10 and while running the server in sql shell (psql) (psql: error: could not connect to server: FATAL: password authentication failed for user "samantha")
This error is showing up
Have you tried this?
psql -U postgres

How to dump a Postgres database if the user has no password set?

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?

Accessing postgres without being root users

I have created a postgres database in postgres named "databaseName". Now I can access this data2database through
su - postgres
and then typing my password
Then I enter into the database through: psql databaseName
I have created users of this database through:
createuser -P userName1
Now I dont want the users of the database to access the database as root user. Now when the users try to login into postgres as
su - postgres -u userName1
or through
psql databaseName -u userName1
I get error...can someone guide me as to how the users can get access to postgres and database without being root user?
Configure pg_hba.conf to accept peer connections over unix sockets or ident connections over host (tcp) connections. If you prefer you can use md5 to use password authentication instead.
By default psql will connect with the same username as your local OS username; this can be overridden by the -U flag, eg:
psql -U myusername thedatabase
Note that it's -U not -u (it's upper case).
This is all covered in detail in the PostgreSQL documentation; see:
Client Authentication
pg_hba.conf
psql
BTW, if you want to run commands as the postgres user, rather than su'ing you can just write:
sudo -u postgres psql

PostgreSQL - Create user and database from shell failing

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