Importing SQL file: PostgreSQL - 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'

Related

'Not valid archive' when using pg_restore

As someone who is very new to working with databases/SQL I'm having trouble setting up a dump from the database Qscored, which can be found at https://zenodo.org/record/4468361#.YgTTZ-7ML0p, the first dump (ab) is the one I've worked with. The downloaded file is of type POSIX tar archive. According to the README file, these are the commands to be run in a terminal window.
cat qscored_dump_25Jan2021ab > qscored_dump_25Jan2021.tar
psql -U postgres
When in a PostGreSQL user run
CREATE DATABASE qscoreddb WITH TEMPLATE=template0 ENCODING='UTF-8';
CREATE USER dbwala WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE qscoreddb TO dbwala;
Then, in a new terminal window I run pg_restore according to
pg_restore -d qscoreddb -U postgres \path\to\qscoreddb_dump_Jan252021.tar
I then get the error message
pg_restore: error: input file does not appear to be a valid archive
Does anyone know how I might be able to solve this problem or does it mean there are problems in the original file?
I'm using PostGreSQL 14 with a Mac Monterey 12.2.

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.

Unable to restore the postgresql data through command prompt

I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan
The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.

How to import existing *.sql files in PostgreSQL 8.4?

I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?
From the command line:
psql -f 1.sql
psql -f 2.sql
From the psql prompt:
\i 1.sql
\i 2.sql
Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:
for f in *.sql ; do psql -f $f ; done
Here's the documentation of the psql application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html
in command line first reach the directory where psql is present then write commands like this:
psql [database name] [username]
and then press enter psql asks for password give the user password:
then write
> \i [full path and file name with extension]
then press enter insertion done.
Well, the shortest way I know of, is following:
psql -U {user_name} -d {database_name} -f {file_path} -h {host_name}
database_name: Which database should you insert your file data in.
file_path: Absolute path to the file through which you want to perform the importing.
host_name: The name of the host. For development purposes, it is mostly localhost.
Upon entering this command in console, you will be prompted to enter your password.
Be careful with "/" and "\". Even on Windows the command should be in the form:
\i c:/1.sql
Always preferred using a connection service file (lookup/google 'psql connection service file')
Then simply:
psql service={yourservicename} < {myfile.sql}
Where yourservicename is a section name from the service file.
enter image description here
use following command :-
C:\Program Files\PostgreSQL\12\bin>psql -U username -d databasename -f D:\file.sql