You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用psql的\i命令执行SQL文件时遭遇Permission denied权限问题

解决psql执行SQL文件时的Permission denied问题

先理清误区

你用的grant all是SQL层面针对数据库对象(表、用户等)的授权命令,和文件系统权限完全无关,没法解决这个报错。

具体解决步骤

  1. 补全chmod命令,确保文件本身有读权限
    你之前的chmod +r没指定目标文件,执行完整命令:

    chmod +r /home/omar/Documents/person.sql
    

    或者直接给所有用户添加读权限:

    chmod o+r /home/omar/Documents/person.sql
    
  2. 检查路径目录的执行权限
    psql要读取文件,必须能遍历从根目录到目标文件的所有目录,每个目录需要有执行权限(x)。执行以下命令检查权限:

    # 检查/home/omar目录权限
    ls -ld /home/omar
    # 检查Documents目录权限
    ls -ld /home/omar/Documents
    

    如果目录权限里没有x(比如显示drwx------),需要给其他用户/组添加执行权限:

    chmod o+x /home/omar
    chmod o+x /home/omar/Documents
    
  3. 验证当前psql用户是否能读取文件
    如果是用postgres用户运行psql(默认情况),直接测试该用户能否读取文件:

    sudo -u postgres cat /home/omar/Documents/person.sql
    

    如果报错,说明postgres用户没权限,可通过以下两种方式解决:

    • 方法一:把文件复制到公共目录(比如/tmp),再执行psql命令:
      cp /home/omar/Documents/person.sql /tmp/
      
      然后在psql里执行:
      test=# \i /tmp/person.sql
      
    • 方法二:修改文件的组权限,让postgres用户能读取:
      chgrp postgres /home/omar/Documents/person.sql
      chmod g+r /home/omar/Documents/person.sql
      
  4. 直接以文件所有者身份执行psql
    如果文件属于omar用户,直接用omar用户运行psql,再执行\i命令:

    psql -d test
    

    然后在psql提示符下执行:

    test=# \i /home/omar/Documents/person.sql
    

内容的提问来源于stack exchange,提问作者Omar Mour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 02:29:54