PostgreSql connecting locally to remote server GCP - java

I am trying to connect locally by using pgAdmin or sql developer to the remote database. Database is on GCP however I dont know what I am missing.
On the screenshot
It is configured in GCP as a Cloud SQL instance and the problem is that initially It did not have public IP only private IP. So I exposed one by following the instruction from: https://cloud.google.com/sql/docs/postgres/authorize-networks#authorized-networks
This is how currently looks like (I am using username and password from the application.properties of a java project that is in VM instance to connect to sqlDeveloper):
screen of the configuration of CloudSql
And the configuration of the sql developer:
sqlDeveloper setup
There is definitely something missing or I am doing it wrong.
Can someone help me with connecting locally to the database.
I am totally new to GCP. I am used to configuration files like in tomcat context.xml

If you haven't a public IP, you can't connect your instance from outside GCP. So you can't (easily) from your local environment.
I wrote an article on this. Have a look, you will understand what's the problem and how to solve it (the hard way!)

Related

Connecting to Private AWS RDS Postgres using Java driver

Wondering the best approach to trying to connect to a Private AWS RDS Postgres instance. For me to connect with pgAdmin Ihad to create an SSH tunnel to the DB. I would like to be able to connect to this DB instance via a Java app from my local box and from our EC2 instances. I think the EC2 instance side won't be hard since they are in the same VPC and I think the security groups can talk, but how do you go about doing a SSH tunnel for the Java drivers?
Or perhaps I am looking at this all incorrectly? BTW this is NOT a Spring application.
Tried to connect directly to the server with pgAdmin. That failed, even with my IP added to it's security group. This is because for security reasons (no public API to the DB server) it is a Private server.
Will just create an API to update the DB when in DEV mode. Kind of an overkill but will keep things secure.

How to merge local database server into a localhost service?

I've been recently learning how databases work and how to write a service that can operate the database. So the situation is following:
I have a MySQL database ran on a localhost server (XAMPP). Accessing the database is simple: localhost:3036/my_database.
In addition to that, I wrote a Java Spring service (actually just copied the sample from https://github.com/spring-guides/gs-accessing-data-mysql.git) that is supposed to be an interface accepting curl requests and based on them to operate the database. So far so good. Everything works. The problem is that this service also runs on a localhost address: localhost:8080.
I would like to implement the MySQL server into my Spring service so that I can just run this service and the database will run and be hosted on the same port. The data is supposed to be available only through this service so I don't want an additional independent MySQL server to be running on a different localhost port.
At the end of the day, I would like to generate an executable jar file that when ran provides me the service on a localhost address and deals with locally stored database seamlessly.
Edit:
I wouldn't like my database to be independent form the service. I don't want to take care of running it and connecting to the service. What I'm searching for is sommething like #jr593 mentioned in the answers below, an embedded database. But is it possible for such databases to save the data locally on the device that the service is running beetwen service runs?
There is a few solution for this.
Change port for java app. It it's Spring Boot you could check property server.port and set it to e.g 8081.
You can chcek what is working on XAMPP on port 8080 and shoud down it (it could be apache or something else)
You don't need XAMPP to have MySQL on local host. You can stop/remove xampp and install standalone MySql, so you will take only port 3036 and nat 8080 and 3036.
You could use Docker with MySql image instead of XAMPP.

Accessing local postgres database via internet

I have a pos software which is developed with java and it is running perfectly on localhost with a PostgreSQL database. But the problem is my client wants to use the pos software while he is travelling.
So my problem is how to access the PostgreSQL database which is running on localhost via internet. I searched this on google and found that I can use web services to connect to my PostgreSQL database via internet. But don't know how to access those web services via internet because I don't have an static IP address.
Can anyone please tell me:
What is the best way to access my localhost PostgreSQL database via internet?
If the best way is using web services how to access those services via internet if I don't have an static IP address?
I'm using java to develop my desktop POS software and it is running on a windows computer.
You would have the same trouble that accessing a box running PostgreSQL: you need to locate that box somehow; it will happens if you deploy your Web Services and it is happening now that you want to access your database. There are many ways to achieve that, but basically you need a permanent connection to the Internet (of course) and a static (real) IP.
I would recommend you to deploy your application in AWS, OpenShift, or Heroku, to name a few.

Creating Database in MySQL in Java netbeans in different machines

I have created a standalone application as a school project. The major problem we are encountering is that since it has Java as front-end and MySQL as back-end (compulsory), and we have created a database that solely belongs to one computer, we cannot run the same project on different machines because it won't have the required database, the tables, or the same username and password we used to connect to MySQL.
So my question is **How can I connect to MySQL server in different Machines? **
For database and Tables, I could run a sql file, but that will happen when I would be connected to the MySQL server. Also I am developing the project at my home computer, and I want to run the project on different computers who are connected to my computer by no means .
You a following options
Shared drive : Attach database stored on remote shared drive to a local SQL Server read here
Connect to remote SQL Server instance from local computer - better if they are in same LAN - Steps here
If you want to package DB with app where you have predefined data and you dont want to save transactional data - use inMemory DB. They will load when you application starts up.
Already your application can work using different machine.
You have only to change the localhost string in your connection string with the IP of the MySQL machine :
DriverManager.getConnection("jdbc:mysql://localhost/database?"+ "user=sqluser&password=sqluserpw");
And make sure that remote access are enabled in your MySQL configuration. You can find more about it here.
Instead of using localhost in your database connection string, you should use the ip or aname of the database host
e.g.
jdbc:mysql://IP:3306/db?user=user&password=password
jdbc:mysql://A-NAME/db?user=user&password=password
Just make sure that port 3306 is open and that you have access on the system the databasse is hosted on

Openshift Connect To MySQL From Local Java Application

I'm using a scalable app on openshift and I'm trying to access its database from a local app but I am having trouble setting up the connection. I need to access my MySql 5.5 database.
IDE: Netbeans 7.4
I am using the environment variable values for the connection. I have no trouble connecting to it using my JBoss app in Openshift. Any help would be much appreciated. Sorry for the incomplete info. I am new here in SOF.
If you have the rhc client installed (see Client Tools Installation Guide if not), you can have it port forward to the server via rhc port-froward. Use localhost as the Host and port number it tells you, with the user name and password supplied from the application page.
Note this will only work when running locally, and the the rhc command will have to be running as well
See also 10.13. Port Forwarding

Categories

Resources