0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Quick Start Guide] PostgreSQL 11 + Rails 6 (on CentOS 7)

Last updated at Posted at 2020-03-06

Install postgresql packages

  • postgresql11.x86_64
  • postgresql11-devel.x86_64
  • postgresql11-libs.x86_64
  • postgresql11-server.x86_64
# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum install postgresql11 postgresql11-devel postgresql11-libs postgresql11-server

Set PATH

# echo 'export PATH=/usr/pgsql-11/bin:$PATH' >> /home/<your user>/.bash_profile
# echo 'export PATH=/usr/pgsql-11/bin:$PATH' >> $HOME/.bash_profile
# export PATH=/usr/pgsql-11/bin:$PATH

Initialize database

# postgresql-11-setup initdb

Edit /var/lib/pgsql/11/data/pg_hba.conf

# sed -i -E '/^#/!s/^/#/g' /var/lib/pgsql/11/data/pg_hba.conf
# echo "local   all   postgres                    trust" >> /var/lib/pgsql/11/data/pg_hba.conf
# echo "host    all   postgres    127.0.0.1/32    trust" >> /var/lib/pgsql/11/data/pg_hba.conf

Start service

# systemctl enable postgresql-11.service
# systemctl start postgresql-11.service

Create rails app

# su - <your user>
$ rails new <app name> --database=postgresql

Edit config/database.yml

Add host, port, and username:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: 127.0.0.1
  port: 5432
  username: postgres

Comment out username and password

production:
  <<: *default
  database: blog_production
  #username: blog
  #password: <%= ENV['BLOG_DATABASE_PASSWORD'] %>

Setup db

$ bin/rails db:setup
$ bin/rails db:migrate

Start server

$ bin/rails server -b 0.0.0.0
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?