简体   繁体   English

使用保存的密码将psql从远程计算机连接到远程postgres数据库

[英]Connect with psql from remote machine to remote postgres database with saved password

I am working on a remote Unix machine where I connect via SSH from my local machine. 我正在使用从本地计算机通过SSH连接的远程Unix计算机。 There is a Postgres database on another host that I need to connect from my remote machine using psql. 我需要使用psql从远程计算机连接到另一个主机上的Postgres数据库。

I would like to be able to run queries from an input file and save the results to an output file, however I did not manage to automatically set the password, surpassing the need to do it every time I run a query. 我希望能够从输入文件中运行查询并将结果保存到输出文件中,但是我没有设法自动设置密码,这超出了我每次运行查询时都要进行的设置。

I tried the following: 我尝试了以下方法:

  1. Run

     psql "user=username password=password host=hostname port=port dbname=database"` 

    with my username , password , hostname , port and database values. 用我的usernamepasswordhostnameportdatabase值。 This worked OK but I did not find a way to add arguments for input/output files (not needing to go through Postgres interactive environment). 这行得通,但是我没有找到为输入/输出文件添加参数的方法(不需要通过Postgres交互式环境)。

  2. According to another SO question , I created a file ~/.pgpass with the following format: hostname:port:database:username:password 根据另一个SO问题 ,我创建了一个文件~/.pgpass ,其格式如下: hostname:port:database:username:password

    and then chmod 0600 ~/.pgpass but when connecting as: psql -h hostname -U username -d database -w psql ignored this (I got the following failed authentication message.) 然后chmod 0600 ~/.pgpass但以以下方式连接时: psql -h hostname -U username -d database -w psql忽略了这一点(我得到了以下失败的身份验证消息。)

    psql: fe_sendauth: no password supplied. psql:fe_sendauth:不提供密码。

    Note that my remote machine's name is different than my username, so this might also create a problem when creating the .pgpass file. 请注意,我的远程计算机的名称与我的用户名不同,因此在创建.pgpass文件时这也可能会产生问题。 Is there any other way to do it? 还有其他方法吗?

Any ideas? 有任何想法吗?

This worked OK but I did not find a way to add arguments for input/output files 这个工作正常,但是我没有找到为输入/输出文件添加参数的方法

You can either redirect the input file to psql, or specify it in -f option: 您可以将输入文件重定向到psql,或在-f选项中指定它:

psql < input.sql
psql -f input.sql

As for output file, just redirect psql's output to it: 至于输出文件,只需将psql的输出重定向到它:

psql -f input.sql > output.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地计算机使用pgadmin4访问外部远程Postgres数据库会导致密码验证失败 - Accessing an external remote postgres database using pgadmin4 from a local machine produce password authentication fail 如何在远程计算机上将Postgres与红宝石连接 - How to connect postgres with ruby on remote machine R RPostgreSQL使用SSL连接到远程Postgres数据库 - R RPostgreSQL Connect to Remote Postgres Database with SSL 从远程机器连接到 RDS Postgres - Connecting to RDS Postgres from remote machine Postgres Docker-无法从远程服务器连接 - Postgres Docker - unable to connect from remote server 无法从远程主机连接到 postgres - Cannot connect to postgres from remote host 如何将 Postgres CLI 中的转储数据库从远程服务器传输到远程服务器或本地计算机? - How can I transfer a dumped database in Postgres CLI from my remote server to my remote server or my local machine? psql-Mac试图连接安装在远程服务器中虚拟机中的Ubuntu系统中的postgreSQL - psql - Mac trying to connect postgreSQL in Ubuntu system installed in a virtual machine in remote server 将数据从Postgres 9.2推送到远程数据库 - Pushing data from Postgres 9.2 to a remote database Postgres 将数据从本地传输到远程数据库 - Postgres transfer data from local to remote database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM