Importing .sql file on windows to postgresql
Asked Answered
A

7

33

I have a .sql file that was created by postgresql a while back. I now want to import this file onto a windows machine running postgresql.

How do I do this. The file is about 1.5gb.

Allusive answered 8/7, 2010 at 13:55 Comment(0)
I
55

You should use psql command line tool:

psql -h hostname -p port_number -U username -f your_file.sql databasename 
Illconditioned answered 8/7, 2010 at 13:59 Comment(1)
what is I want to replace the existing table while loading like this? will it succeed?Cablet
E
26

click on the SQL Shell and log into the database and use import

Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (9.2.4)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=# \i c:/data/data01.sql
Eduard answered 24/8, 2013 at 9:7 Comment(3)
Note inverted "/" symbol, instead of "\" usually used in Windows.Rationality
this is giving me "C:: Permission denied" , i tried changing the disk as well. didn't get fixed even after running it as an administratorOperose
confirm that you are using "/" and not "\" @jakub noted above. It is VERY important. And if you have spaces, enclose the path in quotation marksEduard
C
21

start you psql command tool, it will give you dialog like the following

Server [localhost]:
Database [postgres]:
Port [5432]:yourport
Username [postgres]:
Password for user postgres:**********

then connect to your database

postgres=# \c yourdatabase;

then import the file

yourdatabase=# \i c:/path/path/data/data01.sql

note the / for directory separator & no spaces in file path

Crib answered 3/11, 2017 at 5:8 Comment(0)
B
13

This also works for me:

psql dbname username < file.sql
Briarroot answered 15/6, 2011 at 15:6 Comment(2)
Just a note: the "<" operator won't work in Windows Powershell.Electrothermics
Good point. I've been using cmder for so long I forgot the differences!Briarroot
A
2

command prompt

open your cmd window and type the following (make sure the path of postgres is correct)

."C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h 127.0.0.1 -p 5432 -U postgres -d dbname <./query.sql

Astraea answered 17/4, 2018 at 11:25 Comment(0)
S
2
psql -U <dbusername>
if the prompt makes you enter password, do that.
\c <yourdatabasename>
\i 'thepathusing/delimiter.sql'

Two points you need to watch out that

  • Use / as writing path of the file instead of \.
  • Use single quote symbol ' instead of ".
Sharanshard answered 21/8, 2020 at 15:0 Comment(0)
J
0

If you're doing it with a URI connection string make sure the arguments are before the URI, Powershell examples:

Works on windows:

.\psql -f TestFile.sql $connString

.\psql -c 'SELECT Version();' $connString

Won't work on windows (URI connection before arguments):

.\psql $connString -c 'SELECT Version();' 
Juvenility answered 1/9, 2020 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.