The hardware uses MacBook Air, and the development environment uses VScode.
This is a solution for the error that accompanies the previous article (Making the database PostgreSQL with rails new) (https://qiita.com/satomomo0222/items/e77e200ec6e526076764).
: small_red_triangle_down: Previous content
Specify PostgreSQL as database
$ rails _5.1.6_new app name--database=postgresql
Do this in rails 6
$ rails server
When you start the server with and access [localhost: 3000](http: // localhost: 3000 /), I get the error ** ActiveRecord :: NoDatabaseError **.
This is an error caused by the ** database not existing. ** **
First, change to the directory created by rails new.
$cd created directory
next
$ rails db:create
To generate the database. The database will be automatically generated according to the contents of config / database.yml.
How to do it manually
First, change to the directory created by rails new.
$cd created directory
> Next, rewrite config / database.yml.
>```:config/database.before changing yml
~abridgement~
>development:
<<: *default
database:app name_development
>~abridgement~
~abridgement~
development: <<: *default database:app name_db host: localhost encoding: utf8 reconnect: false username:My username
~abridgement~
> * Since database: is the database name, change development to db for easy understanding.
Others (hots, encoding, recconect, username) can be solved without adding them, but it is recommended to add them.
> ** Next, launch SQL. ** **
>```
$ psql -h localhost -p 5432 -U your username-d postgres
>psql (12.3)
Type "help" for help.
>postgres=#
About commands around here https://www.postgresql.jp/document/9.3/html/app-psql.html It is described in detail in.
($ psql -h localhost -d postgres ← This alone solved the problem.)
Run this command and you will be able to enter SQL statements. I will write the SQL statement to create the database here.
postgres=#
#### **`After input`**
```python
postgres=#create database app name_db;
Here, ** app name \ _db is the database: app name \ _db specified in config / database.yml first. ** **
postgres=#create database app name_db; CREATE DATABASE postgres=# \q
You can exit by typing \ q at the end.
Now start the server again with $ rails server and try accessing localhost: 3000. The error should have been resolved.
# I solved it safely!
If you find it useful, I would be grateful if you could click the ** LGTM button. ** **
Let's do our best to learn Rails together! : raised_hand_tone1:
Recommended Posts