使用命令行参数运行 PostgreSQL .sql 文件 [英] Run a PostgreSQL .sql file using command line arguments

查看:42
本文介绍了使用命令行参数运行 PostgreSQL .sql 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 .sql 文件,其中包含数千条 INSERT 语句,需要在我的 PostgreSQL 数据库上运行这些插入,以便将它们添加到表中.这些文件太大了,无法打开它们并将 INSERT 语句复制到编辑器窗口中并在那里运行.我在 Internet 上发现您可以通过导航到 PostgreSQL 安装的 bin 文件夹来使用以下内容:

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. 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. I found on the Internet that you can use the following by navigating to the bin folder of your PostgreSQL install:

psql -d myDataBase -a -f myInsertFile

就我而言:

psql -d HIGHWAYS -a -f CLUSTER_1000M.sql

然后我被要求为我的用户输入密码,但我无法输入任何内容,当我按 Enter 键时出现此错误:

I am then asked for a password for my user, but I cannot enter anything and when I hit enter I get this error:

psql:致命:用户myUsername"的密码验证失败

psql: FATAL: password authentication failed for user "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?

我通过在我的 pg_hba.conf 文件中添加一个具有以下结构的新条目来解决这个问题:

I got around this issue by adding a new entry in my pg_hba.conf file with the following structure:

# IPv6 local connections:
host    myDbName    myUserName ::1/128    trust

pg_hba.conf 文件通常可以在 PostgreSQL 安装的data"文件夹中找到.

The pg_hba.conf file can usually be found in the 'data' folder of your PostgreSQL install.

推荐答案

您有四种选择来提供密码:

You have four choices to supply a password:

  1. 设置 PGPASSWORD 环境变量.有关详细信息,请参阅手册:
    http://www.postgresql.org/docs/current/static/libpq-envars.html
  2. 使用 .pgpass 文件来存储密码.有关详细信息,请参阅手册:
    http://www.postgresql.org/docs/current/static/libpq-pgpass.html
  3. 对该特定用户使用信任身份验证":http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST
  4. 从 PostgreSQL 9.1 开始,您还可以使用连接字符串:
    https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING

这篇关于使用命令行参数运行 PostgreSQL .sql 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆