Restoring .dump file - "Permission Denied" - postgresql

I am setting up a website and am having some trouble restoring a database .dump file. I am using centos7, selinux, postgresql 9.4, and apache2.
This is my pg_hba.conf file.
This is the command I am trying to move the dump:
psql --single-transaction -U postgres db_name < dump_location
When I do this, I get the error:
Permission denied.
Am I missing something or is there someway I should alter my settings? Let me know if you need more information.
Thank you!

The operating system user you are running your shell as does not have permission to read the path dump_location.
Note that this is not necessarily the operating system user you run psql as. In a statement like:
sudo -u postgres psql mydb < /some/path
then /some/path is read as the current user, before sudo, not as user postgres, because it's the shell that performs the input redirection, not psql.
If, in the above example, you wanted to read the file as user postgres you would:
sudo -u postgres psql -f /some/path mydb
That instructs psql to open and read /some/path when it's started.

Just make sure that you are using correct database user and you have at least read permission on the dump file.
"psql -d -U postgres -f "
will work.

Related

How to run a postgres command: could not identify current directory

I am able to run psql by doing the following:
Davids-d david$ psql --u postgres
Password for user postgres:
psql (9.4.18)
Type "help" for help.
postgres=#
However, when I run the following command, I get an error:
Davids-iMac:datadocs david$ sudo -u postgres psql -f resources/postgresql/initdb.sql
could not identify current directory: Permission denied
What does this mean, and how would I resolve this? Note that I do have the following var set:
david$ echo $PGDATA
/Users/david/PostgreSQL/data/pg94
The issue is the sudo -u postgres.
Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.
We can eliminate psql from the equation by just trying to read the file as the postgres user with sudo -u postgres cat resources/postgresql/initdb.sql. You should get the same error.
There's two things you have to do...
cd to a directory that the postgres user can be in.
Put the file in a place the postgres user can access.
/tmp, for example.
Your command seems wrong, try this:
sudo psql -U postgres -f resources/postgresql/initdb.sql

PostgreSQL: Exporting a database on Windows?

I need to export a database I created to get the code for creating the database and inserting rows.
I understand there is a method of using pg_dump, but all the walkthroughs of using it I can find seem to be on Linux.
Can anyone tell me how to do this on Windows?
You have to execute pg_dump located in the bin folder of your PostgreSQL install.
Ex : C:\Program Files\PostgreSQL\9.4\bin.
The command is pg_dump -U *username* -p *port* -d *database* -W -f *filename*
All the parameters are case sensitive ! (Check your username !)
U is for specifying the user that will connect to the DB.
If you don't specify it, pg_dump will use the login you're logged on with.
p for the port. (Default is 5432)
d for the database name
W to force pg_dump to ask for password
f the name of the file where the export should be stored. If you don't specify this, the dump will be displayed in the console.
Example :
pg_dump -U postgres -p 5432 -d postgres -W -f c:\vm\dump.sql
You may need special permissions to export the file to some folders.
(i.e. : C:\program files\ requires administrative rights for writing.)

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.

Entering a .sql file into a new postgreSQL database

I am having troubles getting started with psql. I can login using the script below
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$
But, once here, I cannot figure out how to find my .sql file.
I tried the options supplied by Bolo here:
How to import existing *.sql files in PostgreSQL 8.4?
But they only give
myusername#ubuntu:~/Desktop/dbscripts$ psql -U root -d first -f myscript.sql
psql: FATAL: Peer authentication failed for user "root"
and
myusername#ubuntu:~/Desktop/dbscripts$ psql -f myscript.sqlp
sql: FATAL: role "myusername" does not exist
and
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$ \i myscript.sql
The program 'i' is currently not installed. To run 'i' please ask your administrator to install the package 'iprint'
This can be handled from any of the above options you tried. In which path do you have myscript.sql? After you do su - postgres, give the full path . So it will be psql -d first -f <pathtosqlfile>/myscript.sql. psql -U root will not work unless you have a user root in the database. Try psql -U postgres. You can do \i sqlscript at psql prompt, not at linux command prompt as you have done. The error you are getting "role "myusername" does not exist" can be avoided either by using -U postgres (or any other db user) or by setting the PGUSER environment variable.

drop db in postgres

I try dropdb mydbname in shell. It do not give any error. But still when I call \l it is still there.
I logged into the postgres server using sudo -u postgres psql.
Other than my main concern I need to know how to go into the database other than just staying outside of it. (as a example if I want to list the tables)
In order to drop database you can use SQL command (but I do not understand why dropdb didn't work) DROP DATABASE mydbname:
sudo -u postgres psql -c "DROP DATABASE mydbname"
It would be good to check if database is not used:
select * from pg_stat_activity where datname = 'mydbname';
The sudo -u postgres psql connects to postgres database. You need to specify database: sudo -u postgres psql mydbname and then you can use metdata commands like \d, \dt, \dv, ...
When you say "shell" ... do you mean the psql shell, not the unix command line shell?
I'd say you're running into this issue:
Postgresql not creating db with “createdb” as superuser, yet not outputting errors
ie you're trying to use dropdb as a psql command, when it's a unix shell command. You're not getting an error because of this:
In psql, why do some commands have no effect?
You didn't terminate the command with a semicolon.
Are you missing the comma(;)? This command worked for me:
drop database <database_name>;
Server should be running, then:
dropdb <database name>
If server is not running, first try:
pg_ctl start -D <mylocal_db_path> -l <mylogfile.log>