-
Notifications
You must be signed in to change notification settings - Fork 23
Setting up Postgres
- 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.
Installation of Postgres is outside the scope of this tutorial.
Below are some suggested resources for installing Postgres locally:
- Mac - Install Postgres with Homebrew.
- Windows - Running and Installing Postgres on Native Windows
- Linux (select your distribution from the top of the linked article) - Postgres Linux downloads
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.
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_
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.
At the root of the valkyrie_pd_demo
app, run the following...
bin/rails valkyrie_engine:install:migrations
bin/rails db:migrate