替换数据库

看到资料表明可能是数据库问题 我就查了如何正确从sqlite3 切换成Postgres

找了一篇参考资料

https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25

其中不熟悉的一个步骤是把 config/database.yml里的内容从


# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

替换成

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: MyTrip_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: MyTrip_test

production:
  <<: *default
  database: MyTrip_production
  username: deploy
  password:

注解掉的说明文字内容还是sqlite3的相关文字没改 先这样吧

以及查资料到后面发现 username: deploy用替换 貌似不是 deploy

因为要改好几处而且看起来string更合理 所以先跳过
报错了 但我不死心

在改 t.string over to t.text前 我想先尝试rake三兄弟

够狠的!三兄弟都执行不了...强啊

第一个文档改动存照 方便改回去

第二个文档改了

t.string :title

第三个文档

t.string  :address
t.string  :price
t.string  :best_time

改了居然还是报错 果然问题出在数据库设置啊啊啊!

尝试解决数据库问题过程中运行了

brew upgrade

看到一些提示 记录以备后用

==> Pouring libxml2--2.9.12.big_sur.bottle.tar.gz
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

隔一段居然还有报错

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/libImath.dylib
Target /usr/local/lib/libImath.dylib
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite imath

To list all files that would be deleted:
  brew link --overwrite --dry-run imath

Possible conflicting files are:
/usr/local/lib/libImath.dylib -> /usr/local/Cellar/ilmbase/2.5.5_1/lib/libImath.dylib

以及

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/OpenEXR/Iex.h
Target /usr/local/include/OpenEXR/Iex.h
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite openexr

To list all files that would be deleted:
  brew link --overwrite --dry-run openexr

Possible conflicting files are:
一堆的文档...就没有复制...

我估计是系统老是自己升级导致的...不要乱升级系统真是老师当时的良心建议啊...

然后发现自己两个数据库都安装了 果然...

==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/13/app-initdb.html

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

最后还有这个眼熟的朋友

==> redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

一通报错折腾之后 找到个能运行起来的解法 https://stackoverflow.com/questions/27700596/homebrew-postgres-broken

rm -rf /usr/local/var/postgres  
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
initdb -D /usr/local/var/postgres

记录有用的信息内容 注意其中我的 user不是云服务器上的那个

The files belonging to this database system will be owned by user "RealAnalysis".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  C
  CTYPE:    UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

注意到user的不同 所以先跑去 config/database.yml把数据库的 user改了

然后就能运行

rake db:create

psql -l

就可以查看数据库情况 比如 owner具体为何

虽然过程没办法完全顺利的按照 https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25 走下来 但是貌似也算顺利替换成pg数据库了?

直接用PG数据库 不用换

真是良心建议 建立以后会部署的项目时 直接执行

rails new my_app_name --database=postgresql

记得my_app_name替换成你的新项目名称哈 这样直接用 pg 数据库就不会有部署的时候要换数据库的吐血了

最后 给你解释一下全程疑惑的一点

解惑一下

还有一篇不错的参考

https://dev.to/forksofpower/make-the-move-from-sqlite3-to-postgres-in-rails-6-34m2

里面有解释为啥换数据库的时候 建议把 t.string 改成t.text

以及查到一个看着挺靠谱的 database.yml 模板当参考

default: &default
  adapter: postgresql
  encoding: unicode
  password: xxxxx #<-- which you have defiend while installing postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: project_development

test:
  <<: *default
  database: project_test

production:
  <<: *default
  database: project_production
  username: username (我不确定要不要改 是否要跟云端的一致)
  password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>

又一堆连环报错 一个个修过去 然后执行了

bundle install --path vendor/bundle

后 提示记录一下 说不定后面可以用

==== Release notes for capistrano-passenger ====
passenger once had only one way to restart: `touch tmp/restart.txt`
Beginning with passenger v4.0.33, a new way was introduced: `passenger-config restart-app`

The new way to restart was not initially practical for everyone,
since for versions of passenger prior to v5.0.10,
it required your deployment user to have sudo access for some server configurations.

capistrano-passenger gives you the flexibility to choose your restart approach, or to rely on reasonable defaults.

If you want to restart using `touch tmp/restart.txt`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, true

If you want to restart using `passenger-config restart-app`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, false # Note that `nil` is NOT the same as `false` here

If you don't set `:passenger_restart_with_touch`, capistrano-passenger will check what version of passenger you are running
and use `passenger-config restart-app` if it is available in that version.

If you are running passenger in standalone mode, it is possible for you to put passenger in your
Gemfile and rely on capistrano-bundler to install it with the rest of your bundle.
If you are installing passenger during your deployment AND you want to restart using `passenger-config restart-app`,
you need to set `:passenger_in_gemfile` to `true` in your `config/deploy.rb`.

好奇心起来 执行了一下

 rake db:migrate RAILS_ENV=production

报错提示为

Caused by:
PG::ConnectionBad: FATAL:  database "MyTrip_production" does not exist

解法

rake db:create:all

就能补上创建不存在的 database "MyTrip_production"

然后就能顺利的跑

 rake db:migrate RAILS_ENV=production

还找到个官网的参考资料 如何设置 config/database.yml

https://guides.rubyonrails.org/configuring.html#configuring-a-postgresql-database

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容