[RUBY] [Rails] rails new to create a database with PostgreSQL

Introduction

This year, I'm trying to challenge the Web little by little, so I'm messing with Rails, which seems to have the most references. For the time being, I've finished building a local environment and are trying various things with Rails new. I'm writing an article with the hope that I'd like to organize my mind once and make it into a template, and I'd be happy if you could point out any mistakes. I hope it will be helpful for those who are having trouble with Rails new.

environment

terminal


Windows10
ruby 2.6.6
Rails 6.0.3.1
psql (PostgreSQL) 12.3

I will not write about environment construction, so please check if necessary. Change the database to PostgreSQL. And gems are basically put locally. It seems that either one is fine, but somehow it does.

Create a Gemfile in your application's directory

Please create a directory appropriately.

terminal


C:\Users\user\sample_app> bundle init

Describe the gem to be added to the Gemfile

Open the created Gemfile with an editor and add the following.

Gemfile


gem 'rails'//Comment out(#)Just remove
gem 'pg'//When changing to PostgreSQL

I'm not sure if I should specify the gem version, so I won't specify it for the time being.

bundle install

terminal


bundle install --path vendor/bundle

--path vendor / bundle is specified when installing the gem locally. This seems to install the gem in vendor / bundle. It seems that --path vendor / bundle can be omitted by specifying only the first time.

rails new

terminal


bundle exec rails new . -d postgresql --skip-turbolinks --skip-test

bundle exec is attached when Rails is installed locally. After that, attach it to all rails commands. -d postgresql --skip-turbolinks --skip-test seems to have many other options, so check it out if necessary. There are many things I don't understand, so I'll just write what everyone is writing.

Create a database in SQL Shell

SQLShell(psql)


create role APPLICATION_NAME with createdb login password 'PASSWORD';
select * from pg_user;

Enter your user name (app name) in APPLICATION_NAME and any password in PASSWORD. Check if the role has been created with select * from pg_user ;.

Update database.yml

Write the following in database.yml in the config folder.

database.yml


・
・
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 } %>

  username: APPLICATION_NAME//add to(Username decided when creating the database)
  password: PASSWORD//add to(Password decided when creating the database)
  host: localhost//add to
・
・

After saving database.yml, update the database as well.

terminal


bundle exec rails db:migrate:reset

Start the server

terminal


bundle exec rails s

In your browser http: // localhost: 3000 / If you access and the image is displayed, it is successful.

rails.png

Recommended Posts

[Rails] rails new to create a database with PostgreSQL
[Rails6] Create a new app with Rails [Beginner]
[Rails 5] Create a new app with Rails [Beginner]
Preparing to create a Rails application
[Rails] Creating a new project with rails new
Create a new app in Rails
Tutorial to create a blog with Rails for beginners Part 1
[Rails] I tried to create a mini app with FullCalendar
A series of steps to create portfolio deliverables with Rails
Tutorial to create a blog with Rails for beginners Part 2
Tutorial to create a blog with Rails for beginners Part 0
[Rails withdrawal] Create a simple withdrawal function with rails
Create a temporary class with new Object () {}
[rails] How to create a partial template
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
Create a team chat with Rails Action Cable
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Introduction] Try to create a Ruby on Rails application
3. Create a database to access from the web module
[Rails] How to create a signed URL for CloudFront
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
How to delete a new_record object built with Rails
How to create a JDBC URL (Oracle Database, Thin)
How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
[Rails] Launch a new project
Migrate GitBucket database to postgreSQL
Create a playground with Xcode 12
How to create a method
Docker command to create Rails project with a single blow in environment without Ruby
I tried to create a java8 development environment with Chocolatey
I made a development environment with rails6 + docker + postgreSQL + Materialize.
[Rails] Create API to download files with Active Storage [S3]
I want to create a form to select the [Rails] category
Create Rails5 and postgresql environment with Docker and make pgadmin available
I tried to create a padrino development environment with Docker
A new employee tried to create an authentication / authorization function from scratch with Spring Security
Create a Vue3 environment with Docker!
Create portfolio with rails + postgres sql
Rails new fails to install mysql
[Rails Tutorial Chapter 5] Create a layout
Create pagination function with Rails Kaminari
Create a database in a production environment
[Rails] How to add new pages
[Java] How to create a folder
Connect to Rails server with iPhone
How to get along with Rails
Try to create a server-client app
Create My Page with Rails devise
Create exceptions with a fluid interface
Introducing React to Rails with react-rails
Rails DB PostgreSQL changed to MySQL
Create a Maven project with a command
I want to add a browsing function with ruby on rails
[PostgreSQL] If you want to delete the Rails app, delete the database first!
[Rails 6] How to create a dynamic form input screen using cocoon