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

psql -d myDataBase -a -f myInsertFile

在我的例子中:

psql -d HIGHWAYS -a -f CLUSTER_1000M.sql

然后我被要求为我的用户输入密码,但我不能输入任何东西,当我按enter键时,我得到这个错误:

psql: FATAL:密码验证失败的用户“myUsername”

为什么它不让我输入密码。是否有一种方法,因为它是至关重要的,我可以运行这些脚本?

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

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

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


当前回答

2021的解决方案

如果你的PostgreSQL数据库在你的系统本地。

psql dbname < sqldump.sql username

如果是在线托管

psql -h hostname dbname < sqldump.sql username

如果你有任何疑问或问题,请在评论中提问。

其他回答

您可以打开命令提示符并以管理员身份运行。然后输入

../bin>psql -f c:/...-h localhost -p 5432 -d databasename -U "postgres"

postgres用户的密码将会显示。

输入密码并输入。我看不清我输入的密码,但这次当我按回车键时,它起作用了。实际上我是在把数据加载到数据库中。

@wingman__7在2021年的答案上有一个小改进:如果你的用户名包含某些字符(在我的例子中是下划线),你需要用-U标志来传递它。 这招对我很管用:

$ psql -h db.host -d db_name -U my_user < query.sql 

通过终端登录到您的数据库,并尝试这样做:

database-# >@pathof_mysqlfile.sql

or

database-#>-i pathof_mysqlfile.sql

or

database-#>-c pathof_mysqlfile.sql

尝试在命令行控制台中使用以下命令:

psql -h localhost -U postgres -f restore.sql 

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

Set the PGPASSWORD environment variable. For details see the manual: http://www.postgresql.org/docs/current/static/libpq-envars.html Use a .pgpass file to store the password. For details see the manual: http://www.postgresql.org/docs/current/static/libpq-pgpass.html Use "trust authentication" for that specific user: http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST Since PostgreSQL 9.1 you can also use a connection string: https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING