How to execute PostgreSQL query using Powershell ISE

Viewed 21

Geeks!, I want to execute the SQL queries below by a script in powershell ISE. This is a sql function already written

select UserAndRoleInsert('test@email.com','VesselName','Master','Vessel Master'); select ClientInsert();

How can I connect with postgres sever using powershell code ? server is 'test123' Database is 'IdentityAdmin_Shore' In localhost

1 Answers

You can try below:

  1. Install Postgresql ODBC driver.

  2. Make sure your system recognizes the driver. For example, if your system is Windows then add DSN in the ODBC Datasource Admin.

    enter image description here 3. Then try executing the code(Though I haven't tested it from my end). You can store the user id and password in a config file.

     $DBConnectionString = "Driver={PostgreSQL Unicode(x64)}:Server=<server>;Port=<port>;Database=$MyDB;Uid=<userid>;Pwd=<pass>;"
     $DBConn = New-Object System.Data.Odbc.OdbcConnection;
     $DBConn.ConnectionString = $DBConnectionString;
     $DBConn.Open();
     $DBCmd = $DBConn.CreateCommand();
     $DBCmd.CommandText = "<query1>;<query2>;";
     $DBCmd.ExecuteReader();
     $DBConn.Close();
    
Related