Heroku postgres populate file permission denied - postgresql

I Get this error message when I try to give my postgres heroku backend an sql file to init the database.
I've tried to change permissions of the file and give it all read and write. I don't know what the problem is.
I suspect it's postgres/sql related but I don't know what to do.
simao#simao-kde-neon:~/Desktop/t1g1$ heroku pg:psql postgresql-colorful-03183 -a differ-backend < init.sql
--> Connecting to postgresql-colorful-03183
could not read from input file: Permission denied
▸ psql exited with code 1

I believe that the Heroku pg utilities use your own local installation of postgres under the hood. Looking at that error, it seems like whichever user runs psql locally doesn't have permissions for init.sql. Try chmod on the sql file to make sure that the correct user has permission to read the file.

Related

PostgreSQL: Error importing csv file from shared network folder

My goal is to import csv file to postgresql database.
my file is located in network shared folder and I do not have no option to make it in a local folder.
My Folder located in :
"smb://file-srv/doc/myfile.csv"
When I run my this PostgreSQL script:
COPY tbl_data
FROM 'smb://file-srv/doc/myfile.csv' DELIMITER ',' CSV;
I would get this error :
ERROR: could not open file "smb://file-srv/doc/myfile.csv" for reading: No such file or directory
SQL state: 58P01
I have no problem to access the file and open it.
I am using PostgreSQL 9.6 under Ubuntu 16.04.
Please Advice how to fix this problem.
Update
When I try to access the file with postgres user I would have same error:
postgres#file-srv:~$$ cat smb://file-srv/doc/myfile.csv
cat: 'smb://file-srv/doc/myfile.csv' : No such file or directory
As I mention when I user mounted folder I created I can access the file.
it is about permission. you have to check read access on file and folders.
also, logging with superuser access may solve your problem.
In short, this is a permissions issue: Your network share is likely locally mounted to your user's UID, while the PostgreSQL server is running as the postgres user.
Second, when you log into your database, there is not an overlap between the database's users and the system's users, even if you have the same username. This means that when you request a file from your network share, the DB user, in this case postgres, does not have the necessary permissions.
To see this, and assuming you have root access on the box in question, you might try to become the postgres user and see that you cannot access the file:
$ sudo su - postgres
$ cat /run/user/.../smb.../yourfile.csv
Permission denied
The fix to your issue will involve -- somehow -- making the file or share accessible to the postgres user. Copying is certainly the quickest way. But that's off the table. You could mount the share (perhaps as read only) as the postgres user. You might do this in fstab.
However, unless this is going to be an automated detail that happens regularly, this seems like heroics. Without more information as to why you can't copy locally, I suggest copying the file locally.

Executing batch file for postgre dbinit with NSIS gives permission denied

Following my previous question I'm now trying to execute a batch file trough NSIS code in order to successfully setup the postgres installation after it is being unzipped. The batch file contains command for initializing the database but it fails because of permission restrictions. I am on a Win7 x64 PC. My user account is the administrator and I start the Setup.exe with Run as adminitrator option. This is the error I get:
C:\Program Files (x86)\Poker Assistant>cd "pgsql\bin"
C:\Program Files (x86)\Poker Assistant\pgsql\bin>initdb -U postgres -A
password
--pwfile "pwd.txt" -E utf8 -D "..\data" The files belonging to this database system will be owned by user "Mandarinite".
This user must also own the server process.
The database cluster will be initialized with locale
"Bulgarian_Bulgaria.1251". initdb: could not find suitable text search
configuration for locale "Bulgarian_ Bulgaria.1251" The default text
search configuration will be set to "simple".
Data page checksums are disabled.
creating directory ../data ... initdb: could not create directory
"../data": Permission denied
EDIT: After tinkering little more with the installer I got to the root of the problem. I cannot in any way execute the following command when the installation is in the Program Files folder:
initdb -U postgres -A password --pwfile "pwd.txt" -E utf8 -D "..\data"
I tried from .bat file. I tried from .cmd file. I tried manually from Command Prompt. I tried start as Administrator. All attempts resulted in the Permission denied error
EDIT2: I did not find any way to fix the problem so I made a workaround. Now I distribute the postgres with its data directory already initialized. Then I only need to create the service and start it.
I just realised what the issue here is.
If you run postgres as Administrator, it uses a special Windows API call to drop permissions (acquire a restricted token), so that it runs without full Administrator rights for security. See PostgreSQL utilities and restricted tokens on windows.
I suspect that what's happening here is that initdb isn't creating the target data directory and setting its permissions before doing that, so it drops permissions and then doesn't have the permissions to create the data directory.
To work around it, simply md ..\data to create the empty directory and then use icacls.exe to grant appropriate permissions before you try to initdb. Or, even better, store it in a more appropriate place like %PROGRAMDATA%\MyApp\pgdata or whatever; application data should not go in %PROGRAMFILES%.

could not open file error with PostgreSQL

I am using PostgreSQL and Centos
While in the the task database I am trying to do this
COPY CUSTOMERS TO '/home/cjones/cfolder/customers.txt' (DELIMITER '|');
I am getting the
Error: could not open file "/home/cjones/customers.txt" for writing: Permission denied
I have done ls -al and chmod the customers.txt to 777 and still getting this error. Any ideas?
Are you aware of all requirements? Per documentation:
1.
You must have select privilege on the table whose values are read by
COPY TO
2.
COPY naming a file or command is only allowed to database superusers,
since it allows reading or writing any file that the server has
privileges to access.
Plus, for the file to be accessible by the server it must lie on the same machine of course. And the directory must be accessible to the user the postgres server runs as, typically postgres (not only the file).
An alternative would be to use the \copy meta-command of psql.

Heroku pg:pull is giving sh: createdb: command not found

When I try to do heroku pg:pull DATABASE_URL myappspassword, I get this error:
my-computer:a-folder (master*) · heroku pg:pull DATABASE_URL myappspassword
! sh: createdb: command not found
!
! Unable to create new local database. Ensure your local Postgres is working and try again.
For once Googling doesn't return a result. I'm wondering if it's related to the fact that when I do which psql, there is no result. Maybe I need to do something special with pgAdmin to get this working (e.g. export command line tools)?
Are you on a Mac, by chance, and using Postgres App?
If so, the problem might be that createdb isn't on your path. Try adding it by inserting the following into ~/.bash_profile.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
Then run source ~/.bash_profile and try again.
There's more info on the Postgres App site: http://postgresapp.com/documentation/cli-tools.html
Your instincts are correct. You need to be able to access the Postgres from your command line. pg:pull is attempting to create a new local database and drop the relevant data from your Heroku database into it. From the docs:
This command will create a new local database named “mylocaldb” and then pull data from database at DATABASE_URL...
Basically, your local machine is trying to run createdb locally but your terminal isn't recognizing it.
I battled this damn error for a few hours. I can't say for certain if this will work for everyone but I found my issue stemmed from running the command via iTerm/ZSH instead of just normal bash. I opened up plain-old vanilla terminal, ran the command, and everything fired off like it should.

HEROKU: What to do with my dump file (PGBACKUPS)

I'm done with the pgbackups process.
First, I installed the pgbackups addon (HEROKU)
via command: heroku addons:add pgbackups
Second, I login using heroku auth:login command
Third, I capture the backup by running the command: heroku pgbackups:capture --app myapp
Fourth, I get the url of the backup by
heroku pgbackups:url b002
So I accessed the url that I saw after running the command: heroku pgbackups: url b002
in this format: https://s3.amazonaws.com/hkpgbackups/app2955630#heroku.com/b002.dump?AWSAccessKeyId=.......
something like that.
After that, I downloaded the file in .dump format
So now, I have the dump file and dont know what to do where to specifically use it...
Can somebody give me an advise to how to use the .dump file?
Please help me. I'm in trouble right now. I've to get a copy of our existing database from heroku.
Your .dump file should be a pg_dump file so you'd need to set up PostgreSQL somewhere and then use pg_restore to load the .dump file into your local database.