0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rails 超初学者がPostgreSQL設定手順を学びました

Last updated at Posted at 2025-03-14

Rails 超初学者の PostgreSQL 設定手順

はじめに

初めまして。
私は、子育て中の現役看護師です。
絶賛エンジニア転職を目指し、Ruby on Rails を勉強しています。
アウトプットのために、PostgreSQLの設定方法を調べながら試してみたので、その手順をまとめました。


環境

  • PostgreSQL 14.15 (Homebrew)
  • Ruby 2.7.7
  • Rails 6.0.6.1

PostgreSQL のインストール

Mac (Homebrew) の場合

brew install postgresql@14
brew services start postgresql@14

インストール確認

psql -U postgres -l

PostgreSQL の初期設定

PostgreSQL の管理ユーザー postgres のパスワードを設定します。

psql -U postgres
ALTER USER postgres WITH PASSWORD 'your_password';
\q  -- 終了

Rails アプリの PostgreSQL 設定

Gemfile の修正

sqlite3pg に変更します。

gem 'pg'

その後、Gem をインストール。

bundle install

config/database.yml の修正

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: your_username
  password: your_password
  host: your_host

development:
  <<: *default
  database: your_development_db

test:
  <<: *default
  database: your_test_db

データベースの作成

rails db:create
rails db:migrate

データベースが作成されたか確認

psql -U postgres -l

動作確認

Rails サーバーを起動し、動作を確認します。

rails s

ブラウザで http://localhost:3000 にアクセスし、
正常に動作すれば完了です。


まとめ

  • ✅ PostgreSQL のインストール
  • postgres ユーザーの設定
  • database.yml の修正
  • ✅ データベースの作成 & マイグレーション
  • ✅ Rails の起動と動作確認

もし間違いや改善点があればコメントで教えてください。
今後も学習のアウトプットとして投稿を続けていきたいと思います。
どうぞ、よろしくお願いいたします。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?