Thank you very much. I just started studying C #, but I want to connect to PostgreSQL, but it doesn't work.
Installed Npgsql and Mono.Security with Nuget package.
I don't know if you can use Mono.Security.

Because I use an internal network, I tried to specify Server name instead of localhost, but I couldn't connect.

Error message When
Server = localhost
System.Net.Sockets.SocketException: 'The connection could not be made because it was rejected by the target computer. '
Server = When a server name is specified
Unhandled exception
System.TimeoutException: 'Operation timed out. '
Applicable source code
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace DB_setuzoku
    class Program
        static void Main (string [] args)
            string conStr;
            conStr = "Server = localhost;" + "Port = 5432;" + "Database = dbname;" + "User Id = id;" + "Password = pass;";
            NpgsqlConnection conn = new NpgsqlConnection (conStr);
            conn.Open ();
            Console.WriteLine ("Connected");
            conn.Close ();
            Console.WriteLine ("Connection closed");

I tried some code.
https://symfoware.blog.fc2.com/blog- entry-1229.html

Supplemental information (FW/tool version etc.)

PostgreSQL 9.2.18
Visual Studio 2017