# How to use PostgreSQL with your Ruby on Rails application on Ubuntu 16.04

Learn how to use [PostgreSQL](https://www.ionos.com/digitalguide/server/know-how/postgresql/ "PostgreSQL") with your [Ruby on Rails](https://www.ionos.com/digitalguide/websites/web-development/ruby-on-rails-creating-your-own-with-less-code/ "Ruby on Rails: creating your own with less code") application, instead of the default SQLite database. SQLite is an easy-to-configure, lightweight product which ships with Ruby on Rails by default. However, PostgreSQL is a more robust solution which provides more advanced features, scaling, and stability, which may make it more suitable for your Ruby on Rails project.

## Requirements

- A Cloud Server running Linux (Ubuntu 16.04)
- PostgreSQL installed and running.
- Ruby on Rails installed and running.
- A basic familiarity with Ruby on Rails.

---

### Note

All of the commands in this tutorial must be issued as the Rails user. This is the user account which you used to install and run Ruby on Rails.

---

## Create a PostgreSQL user

Create a PostgreSQL user so that your Ruby on Rails application will be able to connect to the PostgreSQL database:

```none
sudo -u postgres createuser -s [username]
```

---

### Note

This should be the same username which you used to install and run Ruby on Rails.

---

For example, to create the PostgreSQL username jdoe the command is:

```none
sudo -u postgres createuser -s jdoe
```

To set a password for this user, log in to the PostgreSQL command line client:

```none
sudo -u postgres psql
```

At the PostgreSQL prompt, enter the following command to set the password:

```none
\password [username]
```

For example, to set the password for jdoe the command is:

```none
\password jdoe
```

Enter and confirm the password. Then exit the PostgreSQL client:

```none
\q
```

## Configure the Rails Application

The next step is to enable PostgreSQL support in your Ruby on Rails application.

### Create the Application

First, create the application using the -d postgresql flag:

```none
rails new [application name] -d postgresql
```

For example, the command to create an application named my-app is:

```none
rails new my-app -d postgresql
```

The -d flag tells Ruby on Rails that you will be using PostgreSQL for this application.

### Add the PostgreSQL username and password

Next, move into the directory which Ruby on Rails created for the application:

```none
cd my-app
```

Edit the config/database.yml file:

```none
nano config/database.yml
```

Scroll down to the section which reads:

```none
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: my-app2
```

Delete the # in the last line to un-comment it, and change the username to the one you created:

```none
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
username: jdoe
```

In the next section, delete the # to un-comment the last line, and add the password for the jdoe user:

```none
# The password associated with the postgres role (username).
password: XPmMxZf
```

Save and exit the file.

### Create the new application databases

Use the following rake command to create the databases for your application:

```none
rake db:create
```

## Test the configuration

To test the configuration, simply start the rails application and check it in a browser.

From the application's directory, use the command:

```none
bin/rails s --binding=0.0.0.0
```

---

### Note

Binding the server to 0.0.0.0 allows you to view the application using your server's public IP address.

---

The server should respond with:

```none
[user@localhost my-app]$ bin/rails server
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
```

Switch to a browser and visit "http://your-ip-address:3000". For example, if your IP address is 198.162.0.1 you would go to http://198.162.0:3000.

If all is well and Rails is able to connect to PostgreSQL, you will see the default Rails welcome page.

[![Image: Rails-Grußseite](https://www.ionos.com/digitalguide/fileadmin/_processed_/8/8/csm_rails-splash-page_ba2b63ac24.webp "Rails-Grußseite")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/rails-splash-page.jpg)


This is a markdown version of: [https://www.ionos.com/digitalguide/server/know-how/use-postgresql-with-ruby-on-rails-on-ubuntu/](https://www.ionos.com/digitalguide/server/know-how/use-postgresql-with-ruby-on-rails-on-ubuntu/) for AI/LLM consumption.