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:
-
将 PostgreSQL 添加到 PATH:
在终端中运行以下命令,将 PostgreSQL 的路径添加到你的
PATH中:echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc然后,刷新你的终端配置:
source ~/.zshrc -
设置编译器标志:
如果需要编译依赖于 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 -
启动 PostgreSQL 服务:
如果你希望 PostgreSQL 在后台运行并在登录时自动启动,使用以下命令:
brew services start postgresql@15如果你不需要后台服务,可以手动启动:
LC_ALL="C" /opt/homebrew/opt/postgresql@15/bin/postgres -D /opt/homebrew/var/postgresql@15 -
连接到 PostgreSQL:
使用
psql连接到 PostgreSQL:psql postgres
这样配置后,你应该能够正常使用 PostgreSQL 15。
用户密码
如果默认启动没有设置,你可以手动创建数据库和用户。以下是步骤:
-
启动 PostgreSQL 服务:
确保 PostgreSQL 已经在运行。如果还未启动,可以使用:
brew services start postgresql@15 -
进入 PostgreSQL 命令行:
使用
psql工具:psql postgres -
创建用户和数据库:
在
psql中,执行以下命令:CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;这将创建一个名为
myuser的用户,并创建一个名为mydb的数据库,myuser是该数据库的所有者。 -
退出
psql:输入
\q退出psql。
完成这些步骤后,你就可以使用以下 DSN 连接到数据库 :
dsn := "user=myuser dbname=mydb sslmode=disable password=mypassword"
确保根据你的实际需求调整用户名、数据库名和密码。