首页 > 解决方案 > psql - 无法执行命令 - 权限被拒绝错误

问题描述

尝试使用 psql 运行 SQL 脚本时,出现权限被拒绝错误。

随后的步骤 -

  1. 打开正常的 Bash 交互式 shell
  2. 运行命令如下
sudo -u postgres -s /bin/bash
  1. 尝试运行以下 pgsql - 我提供了对 pega 文件夹的读写访问权限;将 pgsql 文件移动到 postgres 库还需要做什么。
/Library/PostgreSQL/12/bin/psql -U dbusername dbpassword </Users/rr/Documents/pega/pega8db.pgsql

得到以下错误

bash: /Users/rr/Documents/pega/pega8db.pgsql: Permission denied

Ps -ef 的结果

502 129 1 0 上午 8:07 ?? 0:00.09 /Library/PostgreSQL/12/bin/postmaster -D/Library/PostgreSQL/12/data 502 281 129 0 8:07AM ?? 0:00.00 postgres:记录器
502 283 129 0 8:07AM ?? 0:00.01 postgres:检查点
502 284 129 0 8:07AM ?? 0:00.04 postgres:后台编写器
502 285 129 0 8:07AM ?? 0:00.02 postgres: walwriter
502 286 129 0 8:07AM ?? 0:00.04 postgres: autovacuum 启动器
502 287 129 0 8:07AM ?? 0:00.18 postgres:统计收集器
502 288 129 0 8:07AM ?? 0:00.00 postgres:逻辑复制启动器
501 942 1 0 上午 8 点 29 分 ?? 0:11.47 /Library/PostgreSQL/12/pgAdmin 4.app/Contents/MacOS/pgAdmin4 502 947 129 0 8:29AM ?? 0:00.02 postgres: postgres postgres ::1(49607) 空闲 502 949 129 0 8:29AM ?? 0:00.04 postgres: postgres pega ::1(49623) idle 501 957 826 0 8:30AM ttys000 0:00.00 grep -i postgres

标签: postgresqlpsql

解决方案


首先:检查文件本身的权限: ls -al /Users/rr/Documents/pega/pega8db.pgsql 然后您可以通过执行以下操作添加读取和执行权限:( sudo chmod a+rx /Users/rr/Documents/pega/pega8db.pgsql这将为每个人提供读取权限,因此如果它的多用户系统,您可能希望稍后恢复权限)。

您是否能够使用该psql工具连接到数据库并获得 psql 提示符,或者这也失败了?我假设您的.pgsql文件本质上是 sql 脚本,对吧(某种导出)?

尝试像这样运行: psql -d database_name -a -f /Users/rr/Documents/pega/pega8db.pgsql


推荐阅读