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

如何在PGAdmin的psql工具中批量执行多个SQL文件

在PGAdmin的psql工具中批量运行多个SQL文件

你的SQL文件内容

文件1(建表脚本)

create table emp (emp_id int , emp_name text,dept_id int);
create table dept (dept_id int , dept_name text);

注:PostgreSQL建议每条SQL语句末尾加分号;,避免执行报错

文件2(插入数据脚本)

insert into emp values (1,'Mike',100);
insert into emp values (2,'Dave',200);
insert into dept values (100,'sales');
insert into dept values (200,'Marketing');

注:PostgreSQL要求插入语句显式声明values关键字,和Oracle的写法略有区别

操作步骤

  • 打开PGAdmin,连接到目标PostgreSQL数据库
  • 在左侧面板选中目标数据库,右键选择PSQL Tool(或点击顶部工具栏的PSQL图标)打开psql终端
  • 在psql命令行中,使用\i命令加载并运行SQL文件,按顺序执行两个文件:
    \i 'D:/scripts/file1.sql'
    \i 'D:/scripts/file2.sql'
    
    • 路径需使用正斜杠/,若路径含空格必须用单引号包裹;Windows系统也可使用双反斜杠\\
    • 执行后会依次运行建表和插入数据的脚本

更便捷的方式

可以创建一个新的SQL脚本(比如run_all.sql),在其中批量调用两个文件:

-- 执行建表脚本
\i 'D:/scripts/file1.sql';
-- 执行数据插入脚本
\i 'D:/scripts/file2.sql';

然后直接在PGAdmin的查询工具中运行这个脚本,无需手动逐个输入命令。

内容的提问来源于stack exchange,提问作者Udayasoorian CN

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 09:37:14