PostgreSQL Backup/Restore

Continuing my notes publishing … How to backup/restore your PostgreSQL databases? when you recently get the responsibility of a web app? or you will renew your development environment? or you want to reproduce a production error in your machine? or you want to sleep better at night 😉 ? or ….? whatever.

Before to do anything always we should log as postgres user:

sudo su - postgres

Then backup one database:

pg_dump database_name > /tmp/database_name.bak

Or backup them all:

pg_dumpall > /tmp/backup_all.bak

After that you probably want to download them from server to your machine:

scp your.server.com:/tmp/backup_all.bak .

And finally you want to restore one database, but you will need to create the database previously:

createdb -T template0 database_name
psql database_name < database_name.bak

Maybe you also need to create a user and give it proper permissions:

psql
create role user_name with createdb login password 'user_password';

Or restore them all:

psql -f backup_all.bak postgres

This will create all databases and users for you.

Installing Rbenv

RBENV is a Ruby Version manager like RVM. Despite that I’ve been working long time with RVM and it is great, I am giving an opportunity to RBENV. Some reasons:

  • Lighter and more comprehensive to install
  • Simpler, if you use Bundler (I do) you would avoid gemset management

First is install requirements. I’ve got it from RVM, but finally I’ve found that I’ve needed when ruby versions are compiling:

sudo apt-get -y install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison libcurl3 libcurl3-gnutls libcurl4-openssl-dev

Install RBENV:

git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

RUBY-BUILD provides an ‘rbenv install’ command to compile and install different versions of Ruby on UNIX-like systems.

git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc

REHASH avoids use this command every time a gem is installed or uninstalled.

git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

BINSTUBS avoid the use of ‘bundle exec’ in some commands which should be executed in command line:

git clone https://github.com/ianheggie/rbenv-binstubs.git ~/.rbenv/plugins/rbenv-binstubs

The only disadvantage of BINSTUBS is that you have to remember to run the following command

bundle install --binstubs .bundle/bin

in all your actual projects, so that the appropriate bundle version will be accessed. Binstubs will be created at .bundle/bin and won’t interfere with ./bin folder.

Now, you can use it. List all available versions:

rbenv install -l

Install a Ruby version:

rbenv install 2.1.2

Choose default ruby version on your system

rbenv global 2.1.2

Choose default ruby version on your project (this will create a .ruby-version file scanned every time you access the folder)

rbenv local 2.1.2