how to load a sql-file????

Started by markus jaisabout 25 years ago6 messagesgeneral
Jump to latest
#1markus jais
mjais@web.de

hi,
maybe this is somewhere in the docs but I couldn't find
it.
I am a beginner to postgresql and do not know much till now.
I have bought a book on SQL and now I want to
import the sample databases into postgresql
they are provided as *.sql.

in MySQL I can type something like in Bash on my linux box:

$ mysql -u root -p < file.sql

then the file file.sql is read.

can you tell me how to do this with postgresql???
thanks a lot.

markus

--
Markus Jais
http://www.mjais.de
info@mjais.de
The road goes ever on and on - Bilbo Baggins

#2Jason Earl
jdearl@yahoo.com
In reply to: markus jais (#1)
Re: how to load a sql-file????

psql <database_name> -U postgres -f loadfile.sql

Should do what you want. Or if you are already in
psql take a look at the \i command.

Jason

--- markus jais <mjais@web.de> wrote:

hi,
maybe this is somewhere in the docs but I couldn't
find
it.
I am a beginner to postgresql and do not know much
till now.
I have bought a book on SQL and now I want to
import the sample databases into postgresql
they are provided as *.sql.

in MySQL I can type something like in Bash on my
linux box:

$ mysql -u root -p < file.sql

then the file file.sql is read.

can you tell me how to do this with postgresql???
thanks a lot.

markus

--
Markus Jais
http://www.mjais.de
info@mjais.de
The road goes ever on and on - Bilbo Baggins

---------------------------(end of
broadcast)---------------------------
TIP 2: you can get off all lists at once with the
unregister command
(send "unregister YourEmailAddressHere" to

majordomo@postgresql.org)

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/?.refer=text

#3Ron Peterson
rpeterso@mtholyoke.edu
In reply to: Jason Earl (#2)
Re: how to load a sql-file????

Jason Earl wrote:

psql <database_name> -U postgres -f loadfile.sql

Should do what you want. Or if you are already in
psql take a look at the \i command.

Especially if you're just starting, you might like to start psql as
'psql -s'. This puts you in single step mode, so when you use \i, the
SQL commands you are loading will be executed one at a time.

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College
GPG and other info at http://www.mtholyoke.edu/~rpeterso

#4Marek Pętlicki
marpet@buy.pl
In reply to: markus jais (#1)
Re: how to load a sql-file????

On Tuesday, March, 2001-03-27 at 21:42:39, markus jais wrote:

hi,
maybe this is somewhere in the docs but I couldn't find
it.
I am a beginner to postgresql and do not know much till now.
I have bought a book on SQL and now I want to
import the sample databases into postgresql
they are provided as *.sql.

in MySQL I can type something like in Bash on my linux box:

$ mysql -u root -p < file.sql

then the file file.sql is read.

can you tell me how to do this with postgresql???
thanks a lot.

very similar:

psql dbname username < file.sql

should work

(you can well omit username if it is the same as your login name)

regards

--
Marek P�tlicki <marpet@buy.pl>

#5will trillich
will@serensoft.com
In reply to: Marek Pętlicki (#4)
Re: how to load a sql-file????

On Thu, Mar 29, 2001 at 12:17:31AM +0200, Marek P�tlicki wrote:

On Tuesday, March, 2001-03-27 at 21:42:39, markus jais wrote:

hi,
maybe this is somewhere in the docs but I couldn't find
it.
I am a beginner to postgresql and do not know much till now.
I have bought a book on SQL and now I want to
import the sample databases into postgresql
they are provided as *.sql.

in MySQL I can type something like in Bash on my linux box:

$ mysql -u root -p < file.sql

then the file file.sql is read.

can you tell me how to do this with postgresql???
thanks a lot.

very similar:

psql dbname username < file.sql

should work

(you can well omit username if it is the same as your login name)

if your sql is flawless, go right ahead.

since psql reads "<file" from its STDIN, it won't include any
line numbers to help you track down troubles. (as far as it's
concerned, you're just typing reeeeal fast, so the feedback would
occur right as you type, so line numbers would be superfluous.)

on the other hand, both

psql -f file-containing-sql mydb

and

psql mydb
mydb=> \i file-containing-sql

will spit out error messages including line numbers to help you
debug your sql.

--
It is always hazardous to ask "Why?" in science, but it is often
interesting to do so just the same.
-- Isaac Asimov, 'The Genetic Code'

will@serensoft.com
http://newbieDoc.sourceforge.net/ -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

#6Marek Pętlicki
marpet@buy.pl
In reply to: will trillich (#5)
Re: how to load a sql-file????

On Wednesday, March, 2001-03-28 at 16:55:55, will trillich wrote:

On Thu, Mar 29, 2001 at 12:17:31AM +0200, Marek P�tlicki wrote:

On Tuesday, March, 2001-03-27 at 21:42:39, markus jais wrote:

in MySQL I can type something like in Bash on my linux box:

$ mysql -u root -p < file.sql

then the file file.sql is read.

can you tell me how to do this with postgresql???
thanks a lot.

very similar:

psql dbname username < file.sql

should work

(you can well omit username if it is the same as your login name)

if your sql is flawless, go right ahead.

since psql reads "<file" from its STDIN, it won't include any
line numbers to help you track down troubles. (as far as it's
concerned, you're just typing reeeeal fast, so the feedback would
occur right as you type, so line numbers would be superfluous.)

on the other hand, both

psql -f file-containing-sql mydb

and

psql mydb
mydb=> \i file-containing-sql

will spit out error messages including line numbers to help you
debug your sql.

off course, but note that the question was if one could use '< file.sql'

It is the same mechanism as issuing:

my_program_creating_sql | psql dbname username

(for example to use compressed dump to restore without decompressing it
to the disk first) which can't be done with -f option ;-)

Nevertheless your remark about debugging errors in sql files is very
important...

regards

--
Marek P�tlicki <marpet@buy.pl>