【备忘录】【Rails】从安装PostgreSQL到创建数据库的流程如下:

我之前一直使用MySQL,但由于工作需要,要开始使用PostgreSQL。虽然我在本地安装了它,但在创建数据库时,遇到了多次错误,经历了一些困难。所以我会将成功的过程记录在备忘录中。

预先准备

只需一种选项:编辑config/database.yml

我們將 database.yml 文件中的 development 部分編輯如下。

(省略)

development:
  <<: *default
  database: データベース名
  host: localhost
  port: 5432

(省略)

「以下是我所参考的文章」

编辑 .env 文件。

在我工作的项目中,我使用环境变量来配置database.yml文件中默认环境的用户名和密码。
因此,我会创建一个.env文件,并提前输入所需创建数据库的用户名和密码。
(※请根据您自己的情况进行适当更改)

安装至数据库创建。

安装和卸载都使用Homebrew。

brew install postgresql@13 ←バージョン指定しない場合は最新バージョンがインストールされます。
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
Updated 1 formula.
==> Updated Casks
Updated 6 casks.

==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/13/manifests/13.4
Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/9108fa3f85714f66e41fef926869408b2818a5765bcc36779c2f91bcad57705a--postgresql@13-13.4.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/13/blobs/sha256:e9c8001d59522349069422a196365fbfd79706c4abdcac970adf6a60b01aa82b
Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/ffdbdb34a34e20e4f0669d92b0e17fff879b302c848d31b11b01646fb798100e--postgresql@13--13.4.catalina.bottle.tar.gz
==> Pouring postgresql@13--13.4.catalina.bottle.tar.gz
==> Caveats
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgresql@13
For more details, read:
  https://www.postgresql.org/docs/13/app-initdb.html

postgresql@13 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

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

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

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


To start postgresql@13:
  brew services start postgresql@13
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/postgresql@13/bin/postgres -D /usr/local/var/postgresql@13
==> Summary
?  /usr/local/Cellar/postgresql@13/13.4: 3,230 files, 42.8MB

按照返回的文字,执行如下操作。

initdb --locale=C -E UTF-8 /usr/local/var/postgresql@13

然后,会返回以下错误。

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

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: error: directory "/usr/local/var/postgresql@13" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgresql@13" or run initdb
with an argument other than "/usr/local/var/postgresql@13".

按照错误信息中的说明,在下面执行。

rm -rf /usr/local/var/postgresql@13

再次执行以下操作。

initdb --locale=C -E UTF-8 /usr/local/var/postgresql@13
The files belonging to this database system will be owned by user "ユーザー名".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /usr/local/var/postgresql@13 ... 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/Tokyo
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/postgresql@13' -l logfile start

实现目标!在结尾提到了”可以通过以下方式启动数据库服务器”,所以记下启动命令。

启动命令

pg_ctl -D '/usr/local/var/postgresql@13' -l logfile start

结束命令

pg_ctl -D '/usr/local/var/postgresql@13' -l logfile start

为了确认安装的版本,我会检查一下。

psql --version
psql (PostgreSQL) 13.4

接下来,我们将安装pgAdmin4。
pgAdmin4是一种可以直观地查看数据库中存储的数据的工具之一。

brew install pgadmin4
==> Downloading https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v6.0/macos/pgadmin4-6.0.dmg
######################################################################## 100.0%
==> Installing Cask pgadmin4
==> Moving App 'pgAdmin 4.app' to '/Applications/pgAdmin 4.app'
?  pgadmin4 was successfully installed!

我成功了。

那么,让我们输入刚才记录的命令来启动数据库服务器吧。

pg_ctl -D '/usr/local/var/postgresql@13' -l logfile start
waiting for server to start.... done
server started

为PostgreSQL创建一个具有任意名称的用户。

createuser -P 任意のユーザー名

Enter password for new role:パスワードを任意入力(パスワードつけたくない場合はEnterのみ)
Enter it again:パスワードを再度入力(パスワードつけたくない場合はEnterのみ)

我将检查现有的数据库。

psql -l
スクリーンショット 2021-10-17 17.22.28.png

应该有一个名为「postgres」的数据库。
使用下面的命令连接到它。

psql -d postgres

psql (13.4)
Type "help" for help.

postgres=#

在连接到「postgres」后,执行以下命令。
在角色名称一栏中,应该显示用户列表。
其中应该存在先前创建的任意用户名。

postgres=# \du
スクリーンショット 2021-10-17 17.29.19.png

为了在该用户上创建数据库,我们使用以下命令授予数据库创建权限。

postgres=# ALTER ROLE ユーザー名 CREATEDB;
ALTER ROLE

我会确认权限是否已经正确分配。

postgres=# \du

请注意黄色框内。
您已经被授予了创建数据库的权限。

スクリーンショット 2021-10-17 17.34.59.png

现在已经准备好创建数据库了。

在终端上执行以下操作。

bin/rails db:create

然后返回了以下错误。

dyld: lazy symbol binding failed: Symbol not found: _PQresultMemorySize
  Referenced from: /Users/ユーザー名/project/プロジェクト名/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg_ext.bundle
  Expected in: /usr/lib/libpq.5.dylib

dyld: Symbol not found: _PQresultMemorySize
  Referenced from: /Users/ユーザー名/project/プロジェクト名/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg_ext.bundle
  Expected in: /usr/lib/libpq.5.dylib

zsh: abort      bin/rails db:create

解决这个错误的方法在下面的stackoverflow页面上提到了。

参考了以下内容后,我会尝试进行实施。

 pg_ctl -D '/usr/local/var/postgresql@13' -l logfile stop # サーバー停止
 brew install libpq # libpqのインストール
 echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc # パスを通す
 pg_ctl -D '/usr/local/var/postgresql@13' -l logfile start # サーバー起動

那么,再次尝试执行 bin/rails db:create。

bin/rails db:create
Created database 'プロジェクト名_development'
Created database 'プロジェクト名_test'

这次终于成功创建了数据库!

bannerAds