11 Jun 2022
This short guide will install PostgreSQL database on your local Ruby on Rails development environment.
The database will be placed in vendor/postgres*
folder depending on your PostgreSQL version.
Find out your local PostgreSQL version
$ postgres --version
postgres (PostgreSQL) 14.2
Create local database for Postgres
$ pg_ctl init -D vendor/postgresql<version>
Start PostgreSQL on another terminal. You have to have PostgreSQL running in order to perform the following steps.
$ postgres -D vendor/postgresql14.2/
2022-06-11 09:10:20.723 EEST [43595] LOG: starting PostgreSQL 14.2 on aarch64-apple-darwin21.3.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.30), 64-bit
2022-06-11 09:10:20.725 EEST [43595] LOG: listening on IPv6 address "::1", port 5432
2022-06-11 09:10:20.725 EEST [43595] LOG: listening on IPv4 address "127.0.0.1", port 5432
...
...
Log in to PostgreSQL
$ psql -p 5432 -h localhost -d postgres
Create Superuser in Postgres
$ CREATE USER postgres SUPERUSER;
$ \quit
Create portal user for local development and use password password12
.
$ createuser localdev -d -P -s
Depending on your PostgreSQL access control configuration, you might need to username and password configuration to your config/database.yml
file.
# config/database.yml
...
...
development:
...
# Add these two lines
username: localdev
password: password12
...
test:
...
# Add these two lines
username: localdev
password: password12
...
Run Rails task to create the databases. If you see following output, databases for development and test environments were created successfully.
$ rails db:create
Created database '<rails_project_name>_development'
Created database '<rails_project_name>_test'
Add the following line to Procfile.dev
# Procfile.dev
db: postgres -D vendor/postgresql<version>
Add this following line to .gitignore
and commit it to your repository. This way your database will not be commited to the git repository.
# .gitignore
/vendor/postgres*
Finally, start your Rails development engine and you’re good to go!
$ bin/dev