postgreSQL permission denied when reading from file with \i command - postgresql

this is my problem:
myname#ubuntu:~$ sudo su postgres -c "psql template1"
Password:
psql (9.1.6)
Type "help" for help.
template1=# \i /create.sql
/create.sql: Permission denied
I have this problem even when the file is on the Desktop.
when I copy the text of create.sql and paste it there it works.
using UBUNTU 12.10, postgresql 9.1
Thank you for the help.

If you work in windows , you just need to pass the entire path wrapped by a single quotation.
test-# \i 'D:\\person.sql' --- > note here double backslash not single

The problem is that you've launched psql as the user postgres and you haven't granted the postgres user permission to read the SQL file. You'll need to give it permission. The simplest way is to grant world read rights:
chmod a+r create.sql
You can change the default permissions assigned to files by altering your umask; search for more information. Some programs rather annoyingly ignore the umask and set restrictive file permissions anyway, so you always need to know how to grant permissions. See man chmod and man chown.
BTW, you're using a very convoluted method for launching psql as the postgres user. This'll do just fine:
sudo -u postgres psql template1
Note that /create.sql specifies a file named create.sql in root of the file system (/). I doubt this is what you intended.
You probably want to specify it as a relative path (without the leading /) if it's in your home directory, like:
template1=# \i create.sql
or if it's on the desktop and you've started psql in your home directory:
template1=# \i Desktop/create.sql

EDIT: A better solution is to move the SQL file to the tmp folder if you are on linux!
I ran into this same issue on Linux but the accepted solution was not sufficient in my case. I eventually realized that you need to make sure that EVERY folder in the path has the correct permissions.
For example if the home folder does not have the correct permissions it does not matter what permissions you give a file inside of the SQL folder.
/home/username/Documents/SQL

if you are facing same issue after putting quote then use forward slash for paths

Related

How to pass password inline initdb postgres?

I am using postgres 9.3 . I want to make a script to create my database cluster and supply the password inline in the terminal. I know you can do it from file, but is there a way to do it command line?
that is the line I am using right now : 'initdb -D path/to/cluster -W -A password'
it then prompt me for password, I tried to provide it inline, but it does not work. Any ideas?
thanks
You can accomplish this with a shell trick. Assuming bash shell:
initdb -D path/to/cluster -A password --pwfile=<(echo secretpassword)
(Although you should never use -A password, use at least md5.)
As for your comment, it is hard to say what is going on. You don't show us starting the server at all, or setting the port to start on to 5555, nor creating a user named 'dbuser'.
thanks everyone!
It worked when I changed 'password' to 'md5' in my initdb statement.
although in the password.txt file, I can only store the password. If I follow the documentation from postgres
host:port:username:password it does not work anymore
I solved the connection problem with .pgpass. I've created the .pgpass file in the home directory and I was able to connect using: "psql -U username -d database -pXXXX"
Ok, my initial question was to supply the password inline in the terminal. But ,the proper way of doing is using the password file for security reason. Second, in the initdb statement use at least md5 for encryption. The password will be for the superuser for the default database. You statement should look like this:
"initdb -D /path/to/dbCluster -A md5 --pwfile=/path/to/password.txt"
Now, if you want to automatically connect to the database with psql without password prompt, you have to create a .pgpass file (linux) or pgpass.conf file (windows) with your user and password info in this format: host:port:db_name:user_name:password
Where you put those file is important:
Windows : /Users/user_name/AppData/Roaming/postgresql/pgpass.conf
(If the postgresql folder does not exist, you have to create it)
Linux : /home/user/.pgpass (with chmod 0600 permission on the file)
How to force psql to detect .pgpass file on Windows 10 system?

Error saving psql history file

My current psql version is 10.1,
but when I type \q to save the history it shows:
postgres=# \q could not save history to file "/opt/PostgreSQL/9.3/.psql_history": No such file or directory
But I am using Postgres 10.1, how to fix this?
How psql determines the path of the history file is documented as:
HISTFILE
The file name that will be used to store the history list. If unset, the file name is taken from the PSQL_HISTORY environment
variable. If that is not set either, the default is ~/.psql_history,
or %APPDATA%\postgresql\psql_history on Windows
You must be aware thatpsql does not use HOME to figure out the home directory represented by the tidle character, it uses /etc/passwd.
So presumably in the question psql is launched by the postgres user, and when this user was created, it was to install PostgreSQL 9.3 on this machine, and it was not changed afterwards when /opt/PostgreSQL/9.3/ got deleted, so the entry in /etc/passwd still points to that non-existing directory.
This answer on DBA.se give the shell command to fix that:
sudo usermod --home '/path/to/database' postgres

Postgres PSQL Import file from certain location [duplicate]

I'm new to postgreSQL and I have a simple question:
I'm trying to create a simple script that creates a DB so I can later call it like this:
psql -f createDB.sql
I want the script to call other scripts (separate ones for creating tables, adding constraints, functions etc), like this:
\i script1.sql
\i script2.sql
It works fine provided that createDB.sql is in the same dir.
But if I move script2 to a directory under the one with createDB, and modify the createDB so it looks like this:
\i script1.sql
\i somedir\script2.sql
I get an error:
psql:createDB.sql:2: somedir: Permission denied
I'm using Postgres Plus 8.3 for windows, default postgres user.
EDIT:
Silly me, unix slashes solved the problem.
Postgres started on Linux/Unix. I suspect that reversing the slash with fix it.
\i somedir/script2.sql
If you need to fully qualify something
\i c:/somedir/script2.sql
If that doesn't fix it, my next guess would be you need to escape the backslash.
\i somedir\\script2.sql
Have you tried using Unix style slashes (/ instead of \)?
\ is often an escape or command character, and may be the source of confusion. I have never had issues with this, but I also do not have Windows, so I cannot test it.
Additionally, the permissions may be based on the user running psql, or maybe the user executing the postmaster service, check that both have read to that file in that directory.
Try this, I work myself to do so
\i 'somedir\\script2.sql'
i did try this and its working in windows machine to run a sql file on a specific schema.
psql -h localhost -p 5432 -U username -d databasename -v schema=schemaname < e:\Table.sql

pgrouting error when trying to dump data into database

I was just following this tutorial HERE, its about, pgrouting, When I run the following command:
psql -U user -d postgres -f ~/Desktop/pgrouting-workshop/data/sampledata_routing.sql
I get an error saying the following:
/var/lib/postgresql/Desktop/pgrouting-workshop/data/sampledata_routing.sql: No such file or directory
On my desktop I do have a folder pgrouting-workshop, which does contain the folder data and the sql dump file.
So why am I getting this error?
Because your Desktop isn't in the postgres user's home directory, located at /var/lib/postgresql, but is instead located at /home/myusername/Desktop?
Presumably the psql command you're running is under a sudo -u postgres -i shell, so ~/ means the postgres user's home directory.
Use ~myusername/Desktop/blahblah. Note that the postgres user may not have permission to access it; you can chmod go+x ~ ~/Desktop (run as your user, not postgres) to change that.

postgreSQL - psql \i : how to execute script in a given path

I'm new to postgreSQL and I have a simple question:
I'm trying to create a simple script that creates a DB so I can later call it like this:
psql -f createDB.sql
I want the script to call other scripts (separate ones for creating tables, adding constraints, functions etc), like this:
\i script1.sql
\i script2.sql
It works fine provided that createDB.sql is in the same dir.
But if I move script2 to a directory under the one with createDB, and modify the createDB so it looks like this:
\i script1.sql
\i somedir\script2.sql
I get an error:
psql:createDB.sql:2: somedir: Permission denied
I'm using Postgres Plus 8.3 for windows, default postgres user.
EDIT:
Silly me, unix slashes solved the problem.
Postgres started on Linux/Unix. I suspect that reversing the slash with fix it.
\i somedir/script2.sql
If you need to fully qualify something
\i c:/somedir/script2.sql
If that doesn't fix it, my next guess would be you need to escape the backslash.
\i somedir\\script2.sql
Have you tried using Unix style slashes (/ instead of \)?
\ is often an escape or command character, and may be the source of confusion. I have never had issues with this, but I also do not have Windows, so I cannot test it.
Additionally, the permissions may be based on the user running psql, or maybe the user executing the postmaster service, check that both have read to that file in that directory.
Try this, I work myself to do so
\i 'somedir\\script2.sql'
i did try this and its working in windows machine to run a sql file on a specific schema.
psql -h localhost -p 5432 -U username -d databasename -v schema=schemaname < e:\Table.sql