Searching the web for a solution to create a database for my spring project when it doesn't exist, I found this topic here in stackoverflow:
Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
with this stored procedure to acomplish that:
DO
$do$
BEGIN
IF EXISTS (SELECT 1 FROM pg_database WHERE datname = 'mydb') THEN
RAISE NOTICE 'Database already exists';
ELSE
PERFORM dblink_exec('dbname=' || current_database() -- current db
, $$CREATE DATABASE mydb$$);
END IF;
END
$do$
I want run this procedure from my Java code. My initial idea was include this code as a String atribute in this Service class:
@Service
public class InstallService {
private String query = "";
public boolean create_database(String maquina, String usuario, String senha) {
return false;
}
public boolean create_user(String usuario, String senha, String email) {
return false;
}
}
But I just find out this can't be done. Anyone have any sugestion of how to do that inside this class?