PostgreSQL - How to connect to a remote (either LAN or WAN) database?

Hello all,

Can anyone direct me as to how to connect to a remote PostsgreSQL database? To connect to a server on the same computer, I use the following code:

host() = "localhost"
port() =DbPortNo
DatabaseName() = DbName
DatabaseUserName() = DbUser
DatabasePassword() = DbPW

If db.connect then
  //proceed with database operations
  #If TargetConsole Then
    Print  DbName + " PostgreSQL Database Opened and Connected Properly."
  #Elseif TargetDesktop then
    //ModuleInterface.Update_Display(DbName + " PostgreSQL Database Opened and Connected Properly.")
  #endif
  return 0
  
else
  
  #If TargetConsole Then
    Print  DbName + " PostgreSQL Database Did Not Open or Connected Properly. "  + Cstr(db.ErrorCode) + "  " + db.ErrorMessage
  #Elseif TargetDesktop then
    //ModuleInterface.Update_Display(DbName + " PostgreSQL Database Did Not Open or Connected Properly.  " + Cstr(db.ErrorCode) + "  " + db.ErrorMessage)
  #endif
  return  db.ErrorCode
end if

Do I just change the “host” to show the IP of the remote database (if on a LAN)? If so what is the syntax to do so? What about a WAN based server instance?
Thanks,
Tim

yes you “only” have to change the host IP
but you also have to change the postgres server parameters and configurations files to accept a remote connection.
by default, everything is accepted from localhost, but everything is rejected from others.

Thanks Jean.

So what are the parameters that need to be changed to accept a remote connection?

Thank you again!
Tim

https://www.postgresql.org/docs/10/auth-pg-hba-conf.html
and
edit the postgresql.conf file

https://stackoverflow.com/questions/18580066/how-to-allow-remote-access-to-postgresql-database
https://bosnadev.com/2015/12/15/allow-remote-connections-postgresql-database-server/

Thanks guys!
Tim

Just remember there are security risks involved to opening up direct remote access, especially if you allow from anywhere. If you can’t narrowly scope to specific remotes, you may want to consider writing an API middle layer.

Understood. I would do that for an instance where it is operating over the internet for sure.
Thank you again!
Tim