postgresql 如何在 postgres 数据库中执行 .sql 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33943926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to execute .sql files in postgres database
提问by sasikala
I am using postgres database with PostGIS and PGAdmin. I have many .sql files with different sizes like 300MB, 280MB etc to be inserted into database. What is the best way to do it i.e through java code or some psql commands. I am very new to java and postgres database also. Please give me some suggestion.
我在 PostGIS 和 PGAdmin 中使用 postgres 数据库。我有许多不同大小的 .sql 文件,如 300MB、280MB 等要插入到数据库中。什么是最好的方法,即通过 java 代码或一些 psql 命令。我对 java 和 postgres 数据库也很陌生。请给我一些建议。
回答by Tomasz Jakub Rup
Use psql
command line tool:
使用psql
命令行工具:
psql -f file_with_sql.sql
This command executes all commands line-by-line (except when file contains BEGIN…END blocks. In this case commands in blocks executes in transaction). To wrap all commands in transaction use --single-transaction
switch:
该命令逐行执行所有命令(除非文件包含 BEGIN…END 块。在这种情况下,块中的命令在事务中执行)。要将所有命令包装在事务中,请使用--single-transaction
switch:
psql --single-transaction -f file_with_sql.sql
For more options:
更多选择:
psql --help
回答by charan
Just put it on the command line after psql
:
只需将其放在命令行上psql
:
psql example.sql
psql
will take the file and run each line to the server.
psql
将获取文件并将每一行运行到服务器。
If the server is not running on your computer, you will need to specify the hostname to the computer and a username to log into the server with:
如果服务器未在您的计算机上运行,则需要指定计算机的主机名和用户名以登录服务器:
psql -h server.postgres.com -U username example.sql
To send multiple files, just list them all:
要发送多个文件,只需列出它们:
psql example1.sql example2.sql example3.sql