psql:找不到命令 Mac

psql: command not found Mac

我在 http://www.postgresql.org/download/macosx/

上通过图形安装安装了 PostgreSQL

我在我的应用程序中看到它并且在我的应用程序中也有 psql 终端。我需要 psql 在常规终端中为另一个 bash 脚本工作 我正在 运行 宁为一个应用程序。

出于某种原因,当我 运行

psql

在Mac终端中,我的输出是

-bash: psql: command not found

我运行终端中的以下内容:

locate psql | grep /bin

输出为

/Library/PostgreSQL/9.5/bin/psql

然后我编辑了我的 ~/.bash_profile 并将其添加到路径中,如下所示:

export PATH = /Library/PostgreSQL/9.5/bin/psql:$PATH

~/.bash_profile 中唯一的其他东西是 SDK man,它位于脚本的底部,正如它所说的那样。我也尝试将浴缸设置为 /Library/PostgreSQL/9.5/bin/。我也重新启动了我的终端。

如何让 psql 工作?

编辑 添加到 .bashrc 后,当我打开终端时返回此输出

-bash: export: `/Library/PostgreSQL/9.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin': not a valid identifier

.bashrc 中修改您的 PATH,而不是在 .bash_profile 中:

http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

你的 PATH 有点错误。您需要 "the containing directory" 的路径,而不是实际的可执行文件本身。

你的 PATH 应该这样设置:

export PATH=/Library/PostgreSQL/9.5/bin:$PATH

没有额外的 sql 部分。此外,您必须删除等号周围的空格。

关键字:Postgresql、PATH、macOS、OSX、psql

来自 Postgres documentation page:

sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

重新启动您的终端,它就会出现在您的路径中。

2017 年 10 月回答

运行

export PATH=/Library/PostgreSQL/9.5/bin:$PATH

然后重启终端。

打开主文件夹中的文件 .bash_profile。这是一个隐藏文件。

将下面的路径添加到您 .bash_profile 文件中的末尾导出 PATH 行 :/Applications/Postgres.app/Contents/Versions/latest/bin

符号:分隔路径。

示例:

如果文件包含: export PATH=/usr/bin:/bin:/usr/sbin:/sbin

它将变成: export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/latest/bin

如何显示隐藏文件

在终端中,粘贴以下内容:defaults write com.apple.finder AppleShowAllFiles YES

如果有人在 Mojave 或更高版本中使用自制软件:

export PATH=/usr/local/opt/postgresql@9.5/bin:$PATH

如果需要,请更改版本!

对我来说这很有效:

  1. 正在下载应用程序:https://postgresapp.com/downloads.html

  2. 运行 命令来配置 $PATH - 请注意它对我不起作用。 https://postgresapp.com/documentation/cli-tools.html

  3. 如果您使用的是 zsh,请手动将其添加到 .bash_profile 或 .zshrc 文件中:

     cd  # to get to your home folder
     open .bash_profile  # to open your bash_profile
     # Or
     open .zshrc  # to open your zshrc
    

    在您的 bash 配置文件或 zshrc 中添加:

     # Postgres
     export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin
    

    保存文件。重新启动终端。输入 'psql'。完成。

作为 postgreSQL 新手,我发现 postgresql 站点上的 osx 安装说明令人费解。我遇到了各种各样的错误。幸运的是,卸载程序运行良好。

cd /Library/PostgreSQL/11; open uninstall-postgresql.app/

然后我从 brew install 开始,然后是这篇文章 How to setup PostgreSQL on MacOS

现在可以正常使用了。

Mojave,Postgres 是通过

安装的
brew install https://raw.githubusercontent.com/lembacon/homebrew-core/bede8a46dea462769466f606f86f82511949066f/Formula/postgresql@9.6.rb

如何在您的路径中获取 psql:

brew link postgresql@9.6 --force

如果下载并安装了 Postgres,运行这应该可以解决问题:

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee 
/etc/paths.d/postgresapp

重启终端即可使用psql命令。

参考:https://postgresapp.com/documentation/cli-tools.html

如果Postgresql是从官网下载的。安装后,运行使用这些命令帮助我解决了 psql 问题。

使用 cd ~

转到您的主目录

在您的主目录中,运行 ls -a。使用 vim

编辑 .bash_profile 文件

vi .bash_profile 打开 vim 编辑器。

按编辑器上的 i 插入。

添加export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<Version Number>/bin

Version Number 指的是安装在您本地机器上的 postgresql 的版本号。在我的例子中,安装了 版本 12,所以我输入了

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/12/bin .

esc键并按:wq退出编辑器

在您的终端中输入 source .bash_profile 以读取和执行刚刚作为当前 shell 脚本中的参数传递的文件的内容。

运行 psql

terminal result

总结:

  • cd ~
  • vi .bash_profile
  • export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/12/bin 记下版本号
  • exit vim
  • source .bash_profile
  • psql 有效

我知道其他一些人已经提到路径需要更新,但是对我来说它不起作用直到我在引号中添加了路径本身。我不是 100% 确定为什么会发生这种情况,但这是对我有用的命令(在 MacOS 终端上)

export PATH="/usr/local/opt/postgresql@9.5/bin:$PATH"

在我的例子中,我更新了我的 .bash_profile 并添加了 Postgres 路径,如此处的一些答案所示,但终端中的 运行 psql 仍然给我一个未找到的命令错误。

必须更新 .zprofile 文件并添加 Postgres 路径,它成功了!

终端中的步骤:

  1. nano ~/.zprofile
  2. 在 PATH 末尾添加:/Library/PostgreSQL/14/bin:${PATH} 注意:注意版本号
  3. 保存您的更改。 Ctrl + o 然后 Ctrl + x
  4. source .zprofile

您的路径定义不正确。 运行 在您的终端中执行此命令:

echo 'export PATH="$PATH:/Library/PostgreSQL/<your psql version>/bin/"' >> ~/.zshrc
如果您使用的是 MacOS Catalina 或更新版本,

zshrc 适合您。

我在 Mac 上安装了 postgresql@13Homebrew,安装后我按照 zsh 终端中的给定说明进行操作。

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 restart postgresql@13 after an upgrade:
brew services restart 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

可以通过运行查看路径是否添加: open .zshrc
并在 exec zsh -l

之后重启 zsh 终端