如何在 PostgreSQL 8.4 中导入现有的 *.sql 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3393961/
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 import existing *.sql files in PostgreSQL 8.4?
提问by Badr
I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?
我正在使用 PostgreSQL 8.4,并且我有一些 *.sql 文件要导入到数据库中。我怎么能这样做?
采纳答案by Badr
in command line first reach the directory where psql is present then write commands like this:
在命令行中首先到达 psql 所在的目录,然后编写如下命令:
psql [database name] [username]
and then press enter psql asks for password give the user password:
然后按回车 psql 要求输入密码给用户密码:
then write
然后写
> \i [full path and file name with extension]
then press enter insertion done.
然后按回车插入完成。
回答by Bolo
From the command line:
从命令行:
psql -f 1.sql
psql -f 2.sql
From the psql
prompt:
从psql
提示:
\i 1.sql
\i 2.sql
Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash
shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:
请注意,您可能需要按特定顺序导入文件(例如:数据操作前的数据定义)。如果你有bash
shell(GNU/Linux、Mac OS X、Cygwin)并且文件可以按字母顺序导入,你可以使用这个命令:
for f in *.sql ; do psql -f $f ; done
Here's the documentation of the psql
application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html
这是psql
应用程序的文档(谢谢,弗兰克):http: //www.postgresql.org/docs/current/static/app-psql.html
回答by Arslan Ali
Well, the shortest way I know of, is following:
好吧,我所知道的最短方法如下:
psql -U {user_name} -d {database_name} -f {file_path} -h {host_name}
database_name:Which database should you insert your file data in.
database_name:您应该将文件数据插入哪个数据库。
file_path:Absolute path to the file through which you want to perform the importing.
file_path:要通过其执行导入的文件的绝对路径。
host_name:The name of the host. For development purposes, it is mostly localhost
.
host_name:主机名。出于开发目的,它主要是localhost
.
Upon entering this command in console, you will be prompted to enter your password.
在控制台中输入此命令后,系统将提示您输入密码。
回答by sofiane
Be careful with "/" and "\". Even on Windows the command should be in the form:
小心“/”和“\”。即使在 Windows 上,命令也应该采用以下形式:
\i c:/1.sql
回答by Ted Rybicki
Always preferred using a connection service file(lookup/google 'psql connection service file')
始终首选使用连接服务文件(查找/google 'psql 连接服务文件')
Then simply:
然后简单地:
psql service={yourservicename} < {myfile.sql}
Where yourservicename
is a section name from the service file.
yourservicename
服务文件中的节名称在哪里。