help connect unity with postgresql database
hi
i want to connect my game to a database on Heroku server.
i wrote the code and it works when running in visual studio, the connection works and also the queries without errors, but when running from unity I get an error, I'll attach my code and the errors below.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Npgsql;
using System.Data;
public class DBConn : MonoBehaviour
{
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
List<string> dataItems = new List<string>();
// Start is called before the first frame update
void Start()
{
dataItems = SqlConn();
}
// Update is called once per frame
void Update()
{
}
public List<string> SqlConn()
{
string connstring = String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database={4}?ssl=true;sslfactory=org.postgresql.ssl.NonValidatingFactory",
"host", "port", "userid",
"password", "dbname");
Debug.Log("Connect String = " + connstring);
// Making connection with Npgsql provider
NpgsqlConnection conn = new NpgsqlConnection(connstring);
Debug.Log("Connected");
conn.Open();
Debug.Log("after open");
string sql = "SELECT * FROM webuser";
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
ds.Reset();
da.Fill(ds);
dt = ds.Tables[0];
Debug.Log(dt);
conn.Close();
return dataItems;
}
}
the error
NpgsqlException: FATAL: 28000: no pg_hba.conf entry for host "--", user "--", database "--?ssl=true", SSL off
Npgsql.NpgsqlConnector.CheckErrors () (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnector.Open () (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnectorPool.RequestPooledConnectorInternal (Npgsql.NpgsqlConnection Connection) (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnectorPool.RequestPooledConnector (Npgsql.NpgsqlConnection Connection) (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnectorPool.RequestConnector (Npgsql.NpgsqlConnection Connection) (at <2f318a3bcab64a52b3458684e232b435>:0)
Npgsql.NpgsqlConnection.Open () (at <2f318a3bcab64a52b3458684e232b435>:0)
(wrapper remoting-invoke-with-check) Npgsql.NpgsqlConnection.Open()
DBConn.SqlConn () (at Assets/scripts/DBConn.cs:35)
DBConn.Start () (at Assets/scripts/DBConn.cs:16)
i don't know what is wrong when i remove the ssl part i get the same error i've been struggling with it all day any help will be appreciated.
Answer by Duduck · Sep 28, 2020 at 10:57 AM
@ahotoon There is a "?" in the connstring which suppose to be ";" right between "Database={4}?ssl", although I spot that, I have other problem where it just spit out InvalidOperationException: Exception_Ssl_RequestError, which I can't find a real solution for it. If you and answer I will be greatly appreciated.,There is a "?" in the connstring which suppose to be ";" right between "Database={4}?ssl", although I spot that, I have other problem where it just spit out InvalidOperationException: Exception_Ssl_RequestError, which I can't find a real solution for it. If you and answer I will be greatly appreciated.