看到資料表明可能是數(shù)據(jù)庫問題 我就查了如何正確從sqlite3
切換成Postgres
找了一篇參考資料
https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25
其中不熟悉的一個步驟是把 config/database.yml
里的內(nèi)容從
# 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:
注解掉的說明文字內(nèi)容還是sqlite3的相關(guān)文字沒改 先這樣吧
以及查資料到后面發(fā)現(xiàn) username: deploy
用替換 貌似不是 deploy
在改 t.string
over to t.text
前 我想先嘗試rake三兄弟
夠狠的!三兄弟都執(zhí)行不了...強(qiáng)啊
第二個文檔改了
t.string :title
第三個文檔
t.string :address
t.string :price
t.string :best_time
改了居然還是報(bào)錯 果然問題出在數(shù)據(jù)庫設(shè)置啊啊俺饪浮瞎惫!
嘗試解決數(shù)據(jù)庫問題過程中運(yùn)行了
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"
隔一段居然還有報(bào)錯
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:
一堆的文檔...就沒有復(fù)制...
我估計(jì)是系統(tǒng)老是自己升級導(dǎo)致的...不要亂升級系統(tǒng)真是老師當(dāng)時的良心建議啊...
然后發(fā)現(xiàn)自己兩個數(shù)據(jù)庫都安裝了 果然...
==> 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
一通報(bào)錯折騰之后 找到個能運(yùn)行起來的解法 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
記錄有用的信息內(nèi)容 注意其中我的 user
不是云服務(wù)器上的那個
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
把數(shù)據(jù)庫的 user
改了
然后就能運(yùn)行
rake db:create
用
psql -l
就可以查看數(shù)據(jù)庫情況 比如 owner
具體為何
雖然過程沒辦法完全順利的按照 https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25 走下來 但是貌似也算順利替換成pg數(shù)據(jù)庫了杭攻?
真是良心建議 建立以后會部署的項(xiàng)目時 直接執(zhí)行
rails new my_app_name --database=postgresql
記得my_app_name
替換成你的新項(xiàng)目名稱哈 這樣直接用 pg
數(shù)據(jù)庫就不會有部署的時候要換數(shù)據(jù)庫的吐血了
最后 給你解釋一下全程疑惑的一點(diǎn)
還有一篇不錯的參考
https://dev.to/forksofpower/make-the-move-from-sqlite3-to-postgres-in-rails-6-34m2
里面有解釋為啥換數(shù)據(jù)庫的時候 建議把 t.string
改成t.text
以及查到一個看著挺靠譜的 database.yml
模板當(dāng)參考
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'] %>
又一堆連環(huán)報(bào)錯 一個個修過去 然后執(zhí)行了
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`.
好奇心起來 執(zhí)行了一下
rake db:migrate RAILS_ENV=production
報(bào)錯提示為
Caused by:
PG::ConnectionBad: FATAL: database "MyTrip_production" does not exist
解法
rake db:create:all
就能補(bǔ)上創(chuàng)建不存在的 database "MyTrip_production"
然后就能順利的跑
rake db:migrate RAILS_ENV=production
還找到個官網(wǎng)的參考資料 如何設(shè)置 config/database.yml
https://guides.rubyonrails.org/configuring.html#configuring-a-postgresql-database