0

I M new to postgresql database. I have 2 server 1st server is having postgresql database and 2nd server having my dot net application in visual studio 2017. how should i connect my postgesql to my visual studio as both aare in different server.

i install dotconnect for postgresql in my dot net application through nuget packages. but how to acces the postgres database in visual studio??? I also install pgadmin in my 2nd server to access postgresql. is anybody know???

1 Answer 1

1

I guess that you are using Windows in a local network.

  1. You should listen to all addresses OR the IP of your second Server in .../data/postgresql.conf with listen_addresses = '*' (restart of the postgresql-server is required)

  2. Allow connections from your Server in the file .../data/pg_hba.conf e.g. for IPv4 host all all 172.16.8.109/32 md5 OR all host all all 0.0.0.0/0 md5 (be careful with that)

  3. try connect via visual Studio e.g. http://www.npgsql.org/
    string connstring = "Server='server1';Port='5432';User Id='myUser';Password='myPW;Database='myDatabase';";

    // connect to Database              
    NpgsqlConnection conn = new NpgsqlConnection(strConnection);
    conn.Open();

    try
    {               
        // SQL-Command
        string strSQL = "SELECT * FROM yourTable";

        NpgsqlCommand cmd = new NpgsqlCommand(strSQL, conn);
        NpgsqlDataReader dbReader = null;
        dbReader = cmd.ExecuteReader();

        while (dbReader.Read())
        {
            Console.WriteLine(dbReader.GetValue(0).ToString());
        }

    }
    finally
    {
        conn.Close();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

yes sir i already done the 2nd step and the database is accessible in my 2nd server through pgadmin. now in my visual studio i m trying to take a dataset item and wanting to access my database which i created in postgresql but in that connection window i didnt getting postgresql server instance just like sql server or oracle database. i also install pgsql provider through nuget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.