简体   繁体   中英

Connecting a postgres server in linux platform from another linux server

I have placed my jsp file in a server from where I will have to ssh to another linux server and then connect to the postgres server. We do not have the external IP address to connect directly. thats why this looks complicated. Atleast for me. Here is the first connection part of my jsp.

<%@ page language="java" import="java.sql.*" %>
<%@page import="java.io.*"%>

<%
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://10.254.147.162/dbac";
String username = "postgres";
String password = "postgres";
String myDataField = null;

Connection myConnection = null;
PreparedStatement myPreparedStatement = null;
ResultSet rst = null;
Class.forName(driver).newInstance();
myConnection = DriverManager.getConnection(url,username,password);
.
.
.

Here, I have to connect to postgres server whose IP internal address is 10.0.90.1 through 10.254.147.162.

Please let me know how should I ssh from 10.254.147.162 to

You should deal with the ssh'ing outside of the program.

Establish an ssh tunnel to the database by using the following command in Linux:

ssh -fN -L5444:10.0.90.1:5432 10.254.147.162

This will establish a tunnel from localhost port 5444 to the postgres database on 10.0.90.1 . Then, change your url definition to:

String url = "jdbc:postgresql://localhost:5444/dbac";

You can pick any value you prefer for 5444. This allows you also to, for example, set up port 5444 to go to your production database, and port 5445 to go to a test database on some other machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM