Skip to main content

postgresql

安装

brew install postgresql@15

安装日志

This formula has created a default database cluster with:
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@15

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

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

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

To start postgresql@15 now and restart at login:
brew services start postgresql@15
Or, if you don't want/need a background service you can just run:
LC_ALL="C" /opt/homebrew/opt/postgresql@15/bin/postgres -D /opt/homebrew/var/postgresql@15
==> Summary
🍺 /opt/homebrew/Cellar/postgresql@15/15.12: 3,712 files, 63MB
==> Running `brew cleanup postgresql@15`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> postgresql@15
This formula has created a default database cluster with:
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@15

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

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

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

To start postgresql@15 now and restart at login:
brew services start postgresql@15
Or, if you don't want/need a background service you can just run:
LC_ALL="C" /opt/homebrew/opt/postgresql@15/bin/postgres -D /opt/homebrew/var/postgresql@15

配置及启动【部分】

➜ ~ $ echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc

➜ ~ $ source ~/.zshrc
➜ ~ $ brew services start postgresql@15
==> Successfully started `postgresql@15` (label: homebrew.mxcl.postgresql@15)
➜ ~ $

配置及启动【完整】

根据你提供的信息,以下是详细的步骤来配置和启动 postgresql@15

  1. 将 PostgreSQL 添加到 PATH

    在终端中运行以下命令,将 PostgreSQL 的路径添加到你的 PATH 中:

    echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc

    然后,刷新你的终端配置:

    source ~/.zshrc
  2. 设置编译器标志

    如果需要编译依赖于 PostgreSQL 的软件,设置以下环境变量:

    echo 'export LDFLAGS="-L/opt/homebrew/opt/postgresql@15/lib"' >> ~/.zshrc
    echo 'export CPPFLAGS="-I/opt/homebrew/opt/postgresql@15/include"' >> ~/.zshrc

    同样,刷新配置:

    source ~/.zshrc
  3. 启动 PostgreSQL 服务

    如果你希望 PostgreSQL 在后台运行并在登录时自动启动,使用以下命令:

    brew services start postgresql@15

    如果你不需要后台服务,可以手动启动:

    LC_ALL="C" /opt/homebrew/opt/postgresql@15/bin/postgres -D /opt/homebrew/var/postgresql@15
  4. 连接到 PostgreSQL

    使用 psql 连接到 PostgreSQL:

    psql postgres

这样配置后,你应该能够正常使用 PostgreSQL 15。

用户密码

如果默认启动没有设置,你可以手动创建数据库和用户。以下是步骤:

  1. 启动 PostgreSQL 服务

    确保 PostgreSQL 已经在运行。如果还未启动,可以使用:

    brew services start postgresql@15
  2. 进入 PostgreSQL 命令行

    使用 psql 工具:

    psql postgres
  3. 创建用户和数据库

    psql 中,执行以下命令:

    CREATE USER myuser WITH PASSWORD 'mypassword';
    CREATE DATABASE mydb OWNER myuser;

    这将创建一个名为 myuser 的用户,并创建一个名为 mydb 的数据库,myuser 是该数据库的所有者。

  4. 退出 psql

    输入 \q 退出 psql

完成这些步骤后,你就可以使用以下 DSN 连接到数据库:

dsn := "user=myuser dbname=mydb sslmode=disable password=mypassword"

确保根据你的实际需求调整用户名、数据库名和密码。