使用 Powershell 连接到远程 PostgreSql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9217650/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:18:58  来源:igfitidea点击:

Connect to remote PostgreSql database using Powershell

postgresqlpowershellodbc

提问by stirling

I'm trying to connect to a remote PostgreSql database using powershell. This is my first time using powershell so I'm sorry if this is a noob question. This is my Code:

我正在尝试使用 powershell 连接到远程 PostgreSql 数据库。这是我第一次使用 powershell,所以如果这是一个菜鸟问题,我很抱歉。这是我的代码:

$DBConnectionString = "Driver={PostgreSQL UNICODE}:Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM mytable;";
$DBCmd.ExecuteReader();
$DBConn.Close();

When I run this I get "Exception Calling "Open" with "0" argument(s): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". I've downloaded and installed the pgsqlodbc driver but I'm still getting this error. Does anyone have any ideas how I could fix this? I have searched the internet and I'm really not getting anywhere at this point.

当我运行它时,我得到“异常调用“打开”,参数为“0”:错误 [IM002] [Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序”。我已经下载并安装了 pgsqlodbc 驱动程序,但仍然出现此错误。有谁知道我如何解决这个问题?我已经在互联网上进行了搜索,但此时我真的一无所获。

Thanks.

谢谢。

采纳答案by CB.

Check if the DSN exists in ODBC data source. If not you have to create one going to 'Control Panel', 'Admin. Tools', 'Data Sources (ODBC)'. Then select 'Add User DSN'- Select the PostgreSQL driver, and fill in your server and database details. Test connection to check is all ok!

检查 ODBC 数据源中是否存在 DSN。如果不是,您必须创建一个进入“控制面板”、“管理员”。工具”、“数据源 (ODBC)”。然后选择“添加用户 DSN”- 选择 PostgreSQL 驱动程序,并填写您的服务器和数据库详细信息。测试连接检查一切正常!

回答by Thiago Back

Consult: https://odbc.postgresql.org/
Download: https://www.postgresql.org/ftp/odbc/versions/msi/

咨询:https: //odbc.postgresql.org/
下载:https: //www.postgresql.org/ftp/odbc/versions/msi/

Data sources (ODBC) on Windows: Start → Search → odbc → User DSN → Add/Configure

Windows 上的数据源 (ODBC):开始 → 搜索 → odbc → 用户 DSN → 添加/配置

Screenshot ODBC Driver Setup

屏幕截图 ODBC 驱动程序设置

Example :

例子 :

$MyServer = "<ip>"
$MyPort  = "5432"
$MyDB = "<database>"
$MyUid = "<user>"
$MyPass = "<pass>"

$DBConnectionString = "Driver={PostgreSQL UNICODE(x64)};Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
$DBCmd = $DBConn.CreateCommand();
$DBCmd.CommandText = "SELECT * FROM tb_module;";
$DBCmd.ExecuteReader();
$DBConn.Close();

回答by stirling

I found the problem, I thought the Postgresql ODBC driver was installed, but it wasn't. I finally got it to work after finding this site: http://code.google.com/p/visionmap/wiki/psqlODBCThen I followed the instructions above. it works.

我发现了问题,我以为安装了 Postgresql ODBC 驱动程序,但事实并非如此。找到这个网站后,我终于让它工作了:http: //code.google.com/p/visionmap/wiki/psqlODBC然后我按照上面的说明进行了操作。有用。

Thanks for all the help.

感谢所有的帮助。

回答by Micha? Ziomek

You actually have a typo in your connection string after Driver declaration. There is a double colon instead of a semicolon :)

在 Driver 声明之后,您的连接字符串中实际上有一个错字。有一个双冒号而不是分号 :)

Wrong: {PostgreSQL UNICODE} : Server

错误:{PostgreSQL UNICODE}:服务器

Correct: {PostgreSQL UNICODE} ; Server

正确:{PostgreSQL UNICODE}; 服务器