I have an aws ec2 instance that is running postgres on it. I have a file in the same instance that is a csv file so that I can populate the database. When I go into postgres to run the copy file, it is saying that the permission is denied. I am accessing the postgress shell with a superuser. I am not sure why I am getting a permission denied. Here is a screen shot of what I am running and error I am getting.
You don't say what OS user has launched psql, but presumably it's postgres.
It doesn't have the right to read any file inside /home/ec2-user because this directory has permissions drwx------, meaning that only ec2-user or root can look into it (the fact that the CSV file itself has world-wide read permissions is not sufficient, all directories in the hierarchy must have the x bit set to allow traversal).
The most common solutions:
1) chmod 755 /home/ec2-user so postgres can access it.
2) Launch psql under the ec2-user with an explicit -U option to specify a database user. This may also mean that a password is going to be asked, depending on Postgres authentication rules set in pg_hba.conf. You may also edit these rules if you're an admin and they don't match your needs.
3) Put the CSV data files in a dedicated directory that both ec2-user and postgres can read, so typically that would be outside of any /home directory.
Related
I want to set up a PostgreSQL database but initdb hits an authentication error. I seem to be doing as described in docs.
I log in as postgres user with doas -u postgres -s (AFAIK it's same as su - postgres)
cd to /home/user/A/B/postgres.
stat . yields
Access: (0700/drwx------) Uid: ( 965/postgres) Gid: ( 965/postgres)
id yields
uid=965(postgres) gid=965(postgres) groups=965(postgres)
Running initdb -D /home/user/A/B/data results in an error with:
could not change directory to "/home/user/A/B/postgres": Permission denied
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: error: could not access directory "/home/user/A/B/postgres/data": Permission denied
This or similar error is hit with or without the directory data created beforehand and also using both relative and absolute paths. The folders A and B in the path are not owned by postgres.
I'm using postgresql 13.4-6 on manjaro with doas for authentication and sudo uninstalled.
I'm trying to import data from a text file into a table. The table is empty but the schema is there. I run the command
\copy tbl_windspeed from '/home/~~~myname~~~~/Documents/csv_windspeed.txt'
It tells me Permission denied. I check the file's permissions and even set it to chmod 777 so it's now -rwxrwxrwx. I'm not sure what I'm doing wrong. I even tried restarting the server? service postgresql restart
I'm not exactly sure why this worked but I found a solution:
I was logged in as "postgres" and I guess it didn't like that. I created a new user with login, and created that database and tables through that user (so it has owner privileges). Since that new user I created had the same username as my linux user name, I think that's why I was able to import that file.
Can I not use the user "postgres" to import files on my laptop? When I login to psql I login through sudo -u postgres -i, I have to put in a password. So, it knows I am authenticated as the root user. If someone could give me some more info or explain why that is the case that would be appreciated.
I need to run multiple commands on an AWS Postgres RDS instance I have. I don't want to enter the password each time. I'm trying to use the .pgpass file but I'm running into errors. The first time I ran into an error which said 'role "ubuntu username" does not exist'. I logged in as the postgres user and created that username. After this, the error I get said database does not exist. I have a feeling these errors have nothing to do with trying to connect to the AWS RDS instance.
psql --host=<awshost> --port=5432 --username=<awsrdsusername> --password --dbname=<dbname asks for a password and then logs me in after I enter it. Now I put a file in /home/<ubuntuusername> called .pgpass which has <awshost>:5432:<dbname>:<awsrdsusername>:<password>. Permissions for this file are set to 0600. Now when I run psql from the terminal and that produces the error - psql: FATAL: role "<ubuntuusername>" does not exist. These steps are as outlined on this page.
Can someone help me with the steps to get a pgpass file to connect to an AWS RDS instance?
.pgpass doesn't provide connection information. You seem to expect that after you create a .pgpass file, you can run psql without arguments and it'll know where to connect. That is not the case.
The hostname, port, etc you put in .pgpass are there so that PostgreSQL knows which line to look at when matching the connection info to find its password.
If you had to run:
psql -h something
to connect without a .pgpass file, you still have to run the same thing to connect with a .pgpass file.
If you run psql without arguments it'll connect to the local PostgreSQL (if any), using the current unix username as the postgresql username and the database to connect to. That's why you get the error you do.
If you want to change the default connection, you can use environment variables like PGHOST, PGPORT, etc, and/or a .pgservice.conf file.
See the manual to learn more.
How can I set up multiple authentication methods for the same host/database/user rule? I want to be able to log in to my postgres user using both sudo -u postgres psql -U postgres (without having to enter a PostgreSQL password) and psql -U postgres --password. Something like the following in pg_hba.conf:
local all postgres md5
local all postgres peer
I can only get one method or the other working at the same time.
Thanks.
(I am using PostgreSQL 9.1).
Nope. Only one auth method is supported for any given configuration.
I'd love it if Pg could support fall-back authentication, where if an ident check fails it allows md5 auth instead. It doesn't support this at the moment, though, and I suspect (I haven't verified) that a protocol change would be required to support it.
What you can do is store the password in a $HOME/.pgpass file for the postgres system user. Give it mode 0600 so it's only readable by the postgres user and by root, both of whom can get direct access to the database files and configuration anyway. That way you get easy admin and md5 auth. On some systems you may have to set and create a home directory for the postgres user before you can do this. See getent passwd postgres to see if if the postgres user has a homedir and if so, where it is.
(UPDATE: used to read $HOME/.psqlrc - which is useful, but .pgpass is suitable for password storage)
I am uncertain if its the permissions issue or something I am doing wrong and any help will be greatly appreciated.
I have a dedicated Ubuntu server with Postgres DB installed on it. I want to backup the databases, but when I use pg_dump I get permission denied. Can some one help me out?
Here are a few details about my set up.
On postgres: I have the following databases.
postgres,
template0,
template1,
mydb,
mydb1
I have 2 PostgreSQL users
postgres,
mydb (There are no postgresql groups defined yet)
All the other settings for postgres are default values.
On my ubuntu itself, I have 2 users.
user1,
mydb
I logged into the box as root/user1 using SSH, issued the following commands,
$ pg_dumpall > a.sql
I get, -bash: a.sql: Permission denied
Same goes with pg_dump command.
I know I am missing something here. Can some one guide me to properly issue this command?
TIA.
I get, -bash: a.sql: Permission denied
Same goes with pg_dump command.
This looks more like a file permission issue. Does the OS user have the right to write the dump file? Check file/directory permissions.
When you run the command
pg_dumpall > a.sql
then your shell will attempt to create the file a.sql in the current directory. If you (as "user1") do not have permission to create files in the current directory, then first use the cd command to change to a directory where you do have permission. You can use the pwd command to see what the current directory actually is.