Export/Backup a PostgreSQL database

pg_dump -U postgres [nomeDB] > db.dump

0
By: michelem
2009-02-06 15:45:03

These Might Interest You

  • Instead of creating a database backup to file you send the results to a database. Entire database can be copied from server 1 to server 2 without as follows mysqldump -u<username> -p<password> -h<source database host> --database database_name | mysql -u<username> -p<password> -h<destination database host>


    0
    mysqldump -u<username> -p<password> -h<source database host> databasename table1 table2 table_n | mysql -u<user> -p<password> -h<destination database host> databasename
    dexta · 2011-05-19 16:11:21 0
  • This command will "su" the execution of the command to the postgres user(implies that you are already logger as root), and export the result of the query to a file on the csv format. You'll need to adequate the fields and database information to one of your choice/need. Show Sample Output


    0
    # su -c "psql -d maillog -c \"copy (select date,sender,destination,subject from maillog where destination like '%domain.com%') to '/tmp/mails.csv' with csv;\" " postgres
    Risthel · 2013-02-13 13:03:17 0
  • Creates a consistent datapumpt export on an Oracle database with the current sequence number, while the system is running and changes happens on the database.


    0
    expdp user/password FLASHBACK_SCN=$(echo -e "select current_scn from v\$database;" | sqlplus / as sysdba 2>/dev/null| grep [0-9][0-9][0-9][0-9][0-9][0-9]*)
    peshay · 2009-10-01 08:55:20 0
  • I have this on a daily cronjob to backup the commandlinefu.com database from NearlyFreeSpeech.net (awesome hosts by the way) to my local drive. Note that (on my Ubuntu system at least) you need to escape the % signs on the crontab.


    17
    ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
    root · 2009-02-18 21:12:53 2

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: