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

6

119

I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?

Cornet answered 3/8, 2010 at 6:7 Comment(0)
C
84

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.

Cornet answered 3/8, 2010 at 10:41 Comment(3)
I use this too. And it works. But, I change the supplied SQL statements. Previously the existing *.sql didn't use any character ; as the termination of a line. And I also must remove the GO. Do you t hink the sql script is not a psql script?Spoiler
I tried to use \i [full path with extension] but I get the error message .sql no such file or directory. Could you please upload an example. Thanks.Soult
If \i says no such file then it did not find a file at that position. It is best to give the absolute URL. For me on Windows, this commandline worked: \i /tmp/robert/test.sql of course you must have valid SQL commands in that file.Revegetate
V
160

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

Valenciavalenciennes answered 3/8, 2010 at 6:16 Comment(6)
@moon Maybe you don't have access rights to the SQL files? What operating system are you on?Valenciavalenciennes
@moon It's the password associated with your PostgreSQL user (the user-pass pair is stored in PostgreSQL).Valenciavalenciennes
@moon I suggest that you split your problem into three stages: 1) make sure that you can get psql running. 2) make sure your user has the necessary write privileges, such as: CREATE, INSERT, UPDATE, etc. 3) import the SQL files. As far as I understand, you're at stage 1 now.Valenciavalenciennes
@Bolo: Would you put the three stages into the answer?Leila
@Valenciavalenciennes , As you mentioned I'm now in third steps. but how I can import .sql file from my local machine. I tried \i [full path with extension] but I get the error message .sql no such file or directory. Could you please give an example.Soult
Okay, it`s working. How to update the new version of file to the same database? So I have changed the first version of file and now I would like to make this changes also in database from file. Is it possible to do it exaclty the same way? psql -f 1.sql e.g. 1.sql has the same name but include changes inside.Vanessa
C
84

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.

Cornet answered 3/8, 2010 at 10:41 Comment(3)
I use this too. And it works. But, I change the supplied SQL statements. Previously the existing *.sql didn't use any character ; as the termination of a line. And I also must remove the GO. Do you t hink the sql script is not a psql script?Spoiler
I tried to use \i [full path with extension] but I get the error message .sql no such file or directory. Could you please upload an example. Thanks.Soult
If \i says no such file then it did not find a file at that position. It is best to give the absolute URL. For me on Windows, this commandline worked: \i /tmp/robert/test.sql of course you must have valid SQL commands in that file.Revegetate
P
40

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.

Permian answered 27/3, 2015 at 9:55 Comment(0)
C
27

Be careful with "/" and "\". Even on Windows the command should be in the form:

\i c:/1.sql
Corse answered 30/1, 2012 at 15:9 Comment(0)
C
1

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.

Consuetudinary answered 12/11, 2017 at 4:36 Comment(0)
A
0

enter image description here

use following command :-

C:\Program Files\PostgreSQL\12\bin>psql -U username -d databasename -f D:\file.sql

Amuse answered 25/11, 2022 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.