Skip to content

Setting up Postgres

Kate Lynch edited this page Jan 30, 2020 · 8 revisions

back to Introduction

Goals

  • Install Postgres locally.
  • Create the application's database, create the database user and assign permissions in Postgres.
  • Securely store database credentials locally for development (optional but recommended)
  • Connect the application to the database and perform database schema migrations.

Installing Postgres

Installation of Postgres is outside the scope of this tutorial.

Below are some suggested resources for installing Postgres locally:

Create the database and user

Start postgres command line with...

psql postgres

NOTE: To quit, type \q in the command line.

create role _USER_NAME_ with login encrypted password '_PASSWORD_' createdb;
create database valkyrie_pg_demo_development with owner _USER_NAME_ encoding 'utf8';
alter user _USER_NAME_ with SUPERUSER;

NOTE: Substitute a user name and password for _USER_NAME_ and _PASSWORD_, respectively.

Setup .env file (optional but recommended)

To avoid having user name and password in Github, it is recommended that you use the env library gem.

Edit .gitignore and confirm/add the following. Note, it is generally commented out by default.

# Used by dotenv library to load environment variables.
.env

Edit Gemfile and add...

gem 'dotenv-deployment'
gem 'dotenv-rails'

Run bundle install...

bundle install

Create/Edit .env file at the root of the app and add the following making the same substitutions for _USER_NAME_ and _PASSWORD_ as made above.

DATABASE_USERNAME=_USER_NAME_
DATABASE_PASSWORD=_PASSWORD_

Configure database connection information

Edit config/database.yml, update the development settings to use the the following env variables and configuration...

development:
  <<: *default
  database: valkyrie_pg_demo_development
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: localhost
  port: 5432

NOTE: There are various ways to setup a database to work with Rails. You do not have to use the settings presented here, but they are sufficient for the tutorial.

Install and run Valkyrie migrations

At the root of the valkyrie_pd_demo app, run the following...

bin/rails valkyrie_engine:install:migrations
bin/rails db:migrate

Previous | Next

Clone this wiki locally