Connect to remote PostgreSql database using Powershell

后端 未结 5 435
说谎
说谎 2020-12-11 06:13

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:<

相关标签:
5条回答
  • 2020-12-11 06:29

    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.

    0 讨论(0)
  • 2020-12-11 06:43

    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.

    0 讨论(0)
  • 2020-12-11 06:44

    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!

    0 讨论(0)
  • 2020-12-11 06:46

    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

    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();
    
    0 讨论(0)
  • 2020-12-11 06:55

    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
    
    0 讨论(0)
提交回复
热议问题