I want to export my database as a .sql file. Can someone help me? The solutions I have found don't work. A detailed description please. On Windows 7.
-
what OS postgres runs at?– Vao TsunJun 23, 2016 at 7:40
-
Windows 7 is the OS– moxmlbJun 23, 2016 at 8:07
-
postgresql.org/docs/current/static/backup-dump.html– a_horse_with_no_nameJun 23, 2016 at 8:49
2 Answers
pg_dump defaults to plain SQL export. both data and structure.
open command prompt and
run pg_dump -U username -h localhost databasename >> sqlfile.sql
Above command is preferable as most of the times there will be an error which will be something like - ...FATAL: Peer authentication failed for user ...
-
-
1
-
-
-
1Note that you'll run this command on the CLI without logging in with the 'psql' command. You can also add the port number like so:
pg_dump -U username -h localhost -p 5433 dbName >> dumpFile.sql
The file will be created in your 'home' folder using the command above but you can specify the location you want. Sep 7, 2019 at 0:46
In windows, first, make sure the path is added in environment variables PATH
C:\Program Files\PostgreSQL\12\bin
After a successful path adding restart cmd and type command
pg_dump -U username -p portnumber -d dbname -W -f location
this command will export both schema and data
for only schema use -s in place of -W and for only data use -a.
replace each variable like username, portnumber, dbname and location according to your situation everything is case sensitive, make sure you insert everything correctly, and to import
psql -h hostname -p port_number -U username -f your_file.sql databasename
make sure your db is created or creation query is present in .sql file