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

PostgresqlImport

Postgresql Problem Overview


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

Postgresql Solutions


Solution 1 - Postgresql

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

Solution 2 - Postgresql

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.

Solution 3 - Postgresql

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.

Solution 4 - Postgresql

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

\i c:/1.sql

Solution 5 - Postgresql

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBadrView Question on Stackoverflow
Solution 1 - PostgresqlBoloView Answer on Stackoverflow
Solution 2 - PostgresqlBadrView Answer on Stackoverflow
Solution 3 - PostgresqlArslan AliView Answer on Stackoverflow
Solution 4 - PostgresqlsofianeView Answer on Stackoverflow
Solution 5 - PostgresqlTed RybickiView Answer on Stackoverflow