9

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:

$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.

Thanks.

stirling
  • 143
  • 2
  • 3
  • 8

5 Answers5

8

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

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

Screenshot ODBC Driver Setup

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();
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Thiago Back
  • 81
  • 1
  • 2
  • This works for me but my result is array which had header FieldCountr and 5 rows with number 3 there. The result is 3 column with 5 rown but how can I assign it to array? – sixplus4iszen Feb 22 '22 at 14:45
6

You can use psql which comes with postgresql if you happen to have postgresql installed on your client

$dburl="postgresql://exusername:expw@exhostname:5432/postgres"
$data="select * from extable" | psql --csv $dburl | ConvertFrom-Csv

You must have psql in your path or reference it, its within e.g. C:\Program Files\PostgreSQL\12\bin. Should be able to type "psql" and see output within powershell.

dog
  • 1,307
  • 13
  • 10
3

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

Wrong:

{PostgreSQL UNICODE} : Server

Correct:

{PostgreSQL UNICODE} ; Server
Avshalom
  • 8,657
  • 1
  • 25
  • 43
3

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!

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
CB.
  • 58,865
  • 9
  • 159
  • 159
2

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/psqlODBC Then I followed the instructions above. it works.

Thanks for all the help.

stirling
  • 143
  • 2
  • 3
  • 8