0

I have a problem with connection to my PSQL database. I made a program in C# and when I published it and placed it on another PC (set that it gets data from db on PC that I programmed application), it worked perfectly. Now I wanted to transfer that db to another PC that will run 24/7, and I had a struggle doing that, but I made it (manually in the end). Now when db is set on that 24/7 PC, I can't connect to it. For ex. When I start my program from laptop that I used for programming, I get this:

enter image description here

My connection string looks like this:

string Connectionstring = "Server=192.168.130.240;Port=5433;User Id=postgres;" +
"Password=password;Database=postgres;";
NpgsqlConnection conn = new NpgsqlConnection(Connectionstring);
conn.Open();

P.S. One difference that I noticed is that on my laptop that I used to program my connection string was the same only with IP: 127.0.0.1, and whenever I open PSQL it automatically starts and its already logged in, but on this 24/7 PC when I start psql.exe it starts and then shuts down, but when I start SQL Shell(PSQL) I need to put in server,database,port,username and password every time I start it.

If someone has any idea how to fix this, please help because this is last step of my project and I can't get it to work...

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Marko Petričević
  • 333
  • 2
  • 9
  • 20
  • Assuming the machines are on the same network & can see each other - the port is usually 5432, so try that. If no luck ensure there is a rule in the firewall on the machine running the server to allow incoming connections on the port. – Alex K. Mar 27 '17 at 11:27
  • @AlexK. I switched port when I was programming to 5433 coz something was wrong, but I'll try both solutions and get back to you – Marko Petričević Mar 27 '17 at 11:28
  • One possible issue is that you need to configure postgresql to accept non-local connections. You do this by adding your address to pg_hba.conf. https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html – mlinth Mar 27 '17 at 11:36
  • In order to remotely access a PostgreSQL database, you must set the two main PostgreSQL configuration files: postgresql.conf pg_hba.conf check this link https://stackoverflow.com/questions/18580066/how-to-allow-remote-access-to-postgresql-database – Ashish Kamat Mar 27 '17 at 11:38
  • 1
    Also check if firewall exception for incoming request is enabled for port 5432, or which ever port you have configured for database. – Ashish Kamat Mar 27 '17 at 11:40
  • @AshishKamat That firewall setting solved the problem, thank you =), post it as an answer so I can vote it up – Marko Petričević Mar 27 '17 at 11:43

1 Answers1

1

Add firewall Exception for incoming TCP connection of PgSQL Database port. By default, it is 5432 port. This is required for cases, where database is kept on remote machines.

Ashish Kamat
  • 567
  • 4
  • 16