[英]Run a PostgreSQL .sql file using command line arguments
I have some .sql files with thousands of INSERT statements in them and need to run these inserts on my PostgreSQL database in order to add them to a table.我有一些 .sql 文件,其中包含数千个 INSERT 语句,需要在我的 PostgreSQL 数据库上运行这些插入,以便将它们添加到表中。 The files are that large that it is impossible to open them and copy the INSERT statements into an editor window and run them there.
这些文件太大以至于无法打开它们并将 INSERT 语句复制到编辑器窗口并在那里运行它们。 I found on the Internet that you can use the following by navigating to the bin folder of your PostgreSQL install:
我在 Internet 上发现,您可以通过导航到 PostgreSQL 安装的 bin 文件夹来使用以下内容:
psql -d myDataBase -a -f myInsertFile
In my case:就我而言:
psql -d HIGHWAYS -a -f CLUSTER_1000M.sql
I am then asked for a password for my user, but I cannot enter anything and when I hit enter I get this error:然后我被要求为我的用户输入密码,但我无法输入任何内容,当我按 Enter 时出现此错误:
psql: FATAL: password authentication failed for user "myUsername"
psql:致命:用户“myUsername”的密码验证失败
Why won't it let me enter a password.为什么不让我输入密码。 Is there a way round this as it is critical that I can run these scripts?
有没有办法解决这个问题,因为我可以运行这些脚本很重要?
I got around this issue by adding a new entry in my pg_hba.conf file with the following structure:我通过在我的 pg_hba.conf 文件中添加一个具有以下结构的新条目来解决这个问题:
# IPv6 local connections:
host myDbName myUserName ::1/128 trust
The pg_hba.conf file can usually be found in the 'data' folder of your PostgreSQL install. pg_hba.conf 文件通常可以在 PostgreSQL 安装的“数据”文件夹中找到。
Of course, you will get a fatal error for authenticating, because you do not include a user name...当然,你会得到一个验证的致命错误,因为你没有包含用户名......
Try this one, it is OK for me :)试试这个,对我来说没问题:)
psql -U username -d myDataBase -a -f myInsertFile
If the database is remote, use the same command with host如果数据库是远程的,使用与主机相同的命令
psql -h host -U username -d myDataBase -a -f myInsertFile
You should do it like this:你应该这样做:
\i path_to_sql_file
See:看:
You have four choices to supply a password:您有四种选择来提供密码:
Use this to execute *.sql files when the PostgreSQL server is located in a difference place:当 PostgreSQL 服务器位于不同的地方时,使用它来执行 *.sql 文件:
psql -h localhost -d userstoreis -U admin -p 5432 -a -q -f /home/jobs/Desktop/resources/postgresql.sql
-h PostgreSQL server IP address
-d database name
-U user name
-p port which PostgreSQL server is listening on
-f path to SQL script
-a all echo
-q quiet
Then you are prompted to enter the password of the user.然后会提示您输入用户的密码。
EDIT: updated based on the comment provided by @zwacky编辑:根据@zwacky 提供的评论更新
If you are logged in into psql on the Linux shell the command is:如果您在 Linux shell 上登录 psql,命令是:
\i fileName.sql
for an absolute path and对于绝对路径和
\ir filename.sql
for the relative path from where you have called psql.对于您调用 psql 的相对路径。
export PGPASSWORD=<password>
psql -h <host> -d <database> -U <user_name> -p <port> -a -w -f <file>.sql
Via the terminal log on to your database and try this:通过终端登录到您的数据库并尝试以下操作:
database-# >@pathof_mysqlfile.sql
or或者
database-#>-i pathof_mysqlfile.sql
or或者
database-#>-c pathof_mysqlfile.sql
您可以在命令行本身上同时提供用户名和密码。
psql "dbname='urDbName' user='yourUserName' password='yourPasswd' host='yourHost'" -f yourFileName.sql
you could even do it in this way:你甚至可以这样做:
sudo -u postgres psql -d myDataBase -a -f myInsertFile
If you have sudo
access on machine and it's not recommended for production scripts just for test on your own machine it's the easiest way.如果您在机器上具有
sudo
访问权限,并且不建议仅在您自己的机器上测试生产脚本,这是最简单的方法。
Walk through on how to run an SQL on the command line for PostgreSQL in Linux:演练如何在 Linux 中的 PostgreSQL 命令行上运行 SQL:
Open a terminal and make sure you can run the psql
command:打开终端并确保您可以运行
psql
命令:
psql --version
which psql
Mine is version 9.1.6 located in /bin/psql
.我的是 9.1.6 版,位于
/bin/psql
。
Create a plain textfile called mysqlfile.sql
创建一个名为
mysqlfile.sql
的纯文本文件
Edit that file, put a single line in there:编辑那个文件,在里面放一行:
select * from mytable;
Run this command on commandline (substituting your username and the name of your database for pgadmin and kurz_prod):在命令行上运行此命令(将您的用户名和数据库名称替换为 pgadmin 和 kurz_prod):
psql -U pgadmin -d kurz_prod -a -f mysqlfile.sql
The following is the result I get on the terminal (I am not prompted for a password):以下是我在终端上得到的结果(没有提示我输入密码):
select * from mytable;
test1
--------
hi
me too
(2 rows)
2021 Solution 2021解决方案
if your PostgreSQL database is on your system locally.如果您的 PostgreSQL 数据库在本地系统上。
psql dbname < sqldump.sql username
If its hosted online如果它在线托管
psql -h hostname dbname < sqldump.sql username
If you have any doubts or questions, please ask them in the comments.如果您有任何疑问或问题,请在评论中提出。
psql -h localhost -d userstoreis -U admin -p 5432 -a -q -f /home/jobs/Desktop/resources/postgresql.sql
Parameter explanations:参数说明:
-h PostgreSQL server IP address
-d database name
-U user name
-p port which PostgreSQL server is listening on
-f path to SQL script
-a all echo
-q quiet
You can open a command prompt and run as administrator.您可以打开命令提示符并以管理员身份运行。 Then type
然后输入
../bin>psql -f c:/...-h localhost -p 5432 -d databasename -U "postgres"
Password for user postgres:
will show up. Password for user postgres:
将显示。
Type your password and enter.输入您的密码并输入。 I couldn't see the password what I was typing, but this time when I press enter it worked.
我看不到我输入的密码,但这次当我按下回车键时它起作用了。 Actually I was loading data into the database.
实际上我正在将数据加载到数据库中。
I achived that wrote (located in the directory where my script is)我实现了所写的(位于我的脚本所在的目录中)
::someguy@host::$sudo -u user psql -d my_database -a -f file.sql
where -u user
is the role who owns the database where I want to execute the script then the psql
connects to the psql
console after that -d my_database
loads me in mydatabase
finally -a -f file.sql
where -a
echo all input from the script and -f
execute commands from file.sql
into mydatabase
, then exit.其中
-u user
是拥有我要执行脚本的数据库的角色,然后psql
连接到psql
控制台,然后-d my_database
最终将我加载到mydatabase
-a -f file.sql
where -a
echo all input from脚本和-f
将命令从file.sql
执行到mydatabase
中,然后退出。
I'm using: psql (PostgreSQL) 10.12 on (Ubuntu 10.12-0ubuntu0.18.04.1)我正在使用: psql (PostgreSQL) 10.12 on (Ubuntu 10.12-0ubuntu0.18.04.1)
A small improvement in @wingman__7 's 2021 answer: if your username contains certain characters (an underscore in my case), you need to pass it with the -U
flag. @wingman__7 的 2021 年答案的一个小改进:如果您的用户名包含某些字符(在我的情况下是下划线),您需要使用
-U
标志传递它。
This worked for me:这对我有用:
$ psql -h db.host -d db_name -U my_user < query.sql
尝试在命令行控制台中使用以下命令:
psql -h localhost -U postgres -f restore.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.