Note: For Postgres installation on Mac OS X, see How to Install Postgres for Mac OS X.
For this post, we will go through the steps and options available to install Postgres on a Linux based system, specifically on Ubuntu 14.04 LTS. We will cover installation options using
apt-get. Let’s dive in!
Install Postgres Database
All Linux distributions come bundled with an Advanced Packaging Tool or APT, to handle installation and removal of software. This acts as a kind of user interface and simplifies the process of installing software on Debian/Linux distributions.
apt-get is one of the common tools for dealing with packages on Linux systems. You can type
apt-get --help to view usage and available options.
APT relies on repositories that contain free software available for installation. PostgreSQL is available on Ubuntu’s
apt repository by default, thus we can install it easily.
Let’s start by updating our
apt-get’s list of packages:
Enter your admin password when prompted and wait for the process to complete.
Next, we’ll install Postgres by entering the following command in our terminal:
What this does is to install additional modules and useful tools found in the contrib directory of the PostgreSQL distribution.
y when prompted “Do you want to continue? [Y/n]” and wait for the installation to complete.
By default Postgres creates a
postgres user and is the only user who can connect to the server. We’ll want to create ourselves on the server with superuser capabilities with the same name as our login name:
Enter your desired password when prompted.
Next, we’ll have to create a database with the same name as our login name since this is what Postgres expects by default when connecting to the server with your login name:
Navigate to your home directory and enter the following command to create the
.psql_history in order to save your history:
psql on your terminal to connect to the server:
\q to quit and return to your terminal.
Set up Postgres to work with a Rails app
First, install the
Make sure you include the
pg gem in your Gemfile, and run
Now create a Postgres user we’ll use in the Rails app. Use the same username that you use for your development work.
Now you are in Postgres command line, run:
Now, set up your
config/database.yml file to point to your Posgres database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Let’s create the development and test databases:
Now you can run pending migrations, if there are any.