Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Database export to .sql file

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. Thanks

like image 349
moxmlb Avatar asked Jun 23 '16 07:06

moxmlb


People also ask

How do I save a database in PostgreSQL?

Backup a Single PostgreSQL Database To back up, a PostgreSQL database, start by logging into your database server, then switch to the Postgres user account, and run pg_dump as follows (replace tecmintdb with the name of the database you want to backup). By default, the output format is a plain-text SQL script file.


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 ...

like image 105
Vao Tsun Avatar answered Sep 20 '22 22:09

Vao Tsun


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

like image 24
Chirag goyal Avatar answered Sep 20 '22 22:09

Chirag goyal