Creating and preparing database postgres "no such file or directory" - postgresql

During preparing database according to How to import OpenStreetMap data into PostgreSQL i always get error message C:/Program: No such file or directory after writing psql -U postgres -d gis -f PATH_TO_POSTGRES/share/contrib/postgis-1.5/postgis.sql In my case path looks like : C:\Program Files\PostgreSQL\9.3\share\contrib\postgis-2.1\postgis.sql
Thank you for help or advices

psql -U postgres -d gis
-f "C:\Program Files\PostgreSQL\9.3\share\contrib\postgis-2.1\postgis.sql"
Here Program Files is separated with space so you need to add double quote around the path.

Related

Importing SQL file: PostgreSQL

I'm a web development student and struggling to solve this problem on my own, any help would be appreciated!
Note that I'm working in an Amazon Cloud9 instance. My problem is that when I try to import an SQL file into a PostgreSQL database using the following command:
$ psql -d my_database < file_to_import.sql
I get the following error:
https:/raw.githubusercontent.com/...[removed for privacy]: No such file or directory
I know the file exists, because I'm able to navigate to it. I've tried copying the contents of the file into a new file on my desktop and then inserting the path to that file in the place of "file_to_import.sql" but that's not working either. I get the same error.
I've also tried importing via this template:
my_database=# \i ~/some/files/file_to_import.sql
But I get the same error. What's gone wrong here?
Thanks in advance!
These issues can be occurred because lack of permissions for a file try one of the following commands with the proper elevated permissions. For number one, you don't need sudo but if that didn't work try the second one of them should help you
1. psql -h hostname -d databasename -U username -f file.sql
2. sudo -u postgres psql db_name < 'file_path'

how to import dvd rental database on mac. The postgres tutorial only gives instructions for windows

I have downloaded the dvd rental.tar file on my desktop. followed the below instructions
pwd
/Users/O2/tools/postgres
mkdir dvdrentaldb
initdb dvdrentaldb
I get the following message
initdb directory "dvdrentaldb' exists but is not empty If you want to create a new database system either remove or empty the directory or run initdb with an argument other than "dvdrentaldb"
I have downloaded the dvd rental.tar file on my desktop. followed the below instructions
pwd
/Users/O2/tools/postgres
mkdir dvdrentaldb
initdb dvdrentaldb
post this i am using the following code
pg_restore -U O2 -d dvdrentaldb dvdrental.tar
i get syntax error at or near "pg_restore"
I get the following message
initdb directory "dvdrentaldb' exists but is not empty If you want to create a new database system either remove or empty the directory or run initdb with an argument other than "dvdrentaldb"
Got this from reddit
https://www.reddit.com/r/PostgreSQL/comments/93uuhg/can_anyone_get_this_sample_database_tutorial_to/
createuser -s dvdrental
createdb dvdrental -O dvdrental
pg_restore -U dvdrental -d dvdrental ./dvdrental.tar

How to import a sample DB into postgres?

According to a website I can download their sample file dvdrental.zip, but
The database file is in zipformat ( dvdrental.zip) so you need to extract > it to dvdrental.tar
First of all, what is a tar? I thought it had to be tar.gz to be compressed? I don't even know how to create a "tar" by itself. I tried:
tar -zcvf dvdrental.tar.gz dvdrental
and
tar -cf dvdrental.tar dvdrental
I try to import with pgAdmin 4 and I get either:
pg_restore: [archiver] input file does not appear to be a valid archive
or
pg_restore: [tar archiver] could not find header for file "toc.dat" in tar archive
respectively. Now, don't ask me why a popular tutorial site created a file in the wrong format. But, can you tell me how to repackage this file so I can use it as a sample DB?
Using Mac OS 10.12.4. Postgres 9.6. And PgAdmin 4 (not sure if it's in beta? It crashed and does all kinds of nonsensical window movement and highlighting)
I have extracted .zip archive first. Then opened pgAdmin and followed the guide "Load the DVD Rental database using the pgAdmin"
https://www.postgresqltutorial.com/load-postgresql-sample-database/
Pay attention to changing 'Format' field from 'Custom or Tar' to 'Directory'. Then you should be able to restore DB.
If you look into the .tar archive you will find the restore.sql where at the top:
-- File paths need to be edited. Search for $$PATH$$ and
-- replace it with the path to the directory containing
-- the extracted data files.
So to create sample DB you could to extract .tar content somewhere and use single command:
sed -e 's/\$\$PATH\$\$/\/path\/to\/extracted\/files/g' restore.sql | psql
Or
sed -e 's/\$\$PATH\$\$/\/path\/to\/extracted\/files/g' restore.sql > r.sql
and try to execute the r.sql content using PgAdmin.
get sample dataset from the link you cited and save somewhere.
Assuming postgres is installed and running do the following:
Run createdb dvdrental
Run pg_restore -d dvdrental ./dvdrental where "./dvdrental" is the path to the downloaded and unzipped file.
For create sample DB in postgres you following this steps:
1.- Create directory and enter it:
mkdir -p /tmp/dvdrental && cd /tmp/dvdrental
2.- Download zip file dvdrental.zip:
wget https://www.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip
3.- Uncompress file .zip and later .tar:
unzip dvdrental.zip
tar -xvf dvdrental.tar
4.- Replace in execution time $$PATH variable and review it with grep command:
sed -e 's/\$\$PATH\$\$/\/tmp\/dvdrental/g' restore.sql | grep --color dvdrental
5.- Import DB sample for specific host (localhost), port (5433), user (db) and database name (postgres):
sed -e 's/\$\$PATH\$\$/\/tmp\/dvdrental/g' restore.sql | psql -h localhost -p 5433 -U db -d postgres
Finally, I show import successful with program pgAdmin III

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.