godot and postgreSQL

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Görf

Hey,

I consider to change from unity to godot.
My next project needs a small dedicated server, which settles on a postgreSQL - database (it is my main-database, which I can manage central across all project (dumps, administration,…).
I searched for postgreSQL-Support but the last answer I find was a “no, not yet” from 2018. Is there any update of this answer?
Is it possible to include e.g. Npgsql in a godot-project?

I am sorry for such a “beginner”-question. I read a few comparison between unity and godot and with some experience in my mind (love2d, .NET) I want to switch - but with my favorite database for server :slight_smile:

Thanks for reading and I wish you a great sunday.

From this i guess you can include c# libraries, so it should work, but i’ll let someone who knows for sure answer.

RazorSh4rk | 2020-05-24 19:01

First, I had to install the mono-version for c#-support.
My test-project throw out a build-error, that the mbuild couldn’t find the .Net-Framework, but it was installed.
After installing the whole Visual-Studio-Package or editing the csproj I got the same error.
Before giving up for the day I decided to create a brand new godot-project. This one I could build.
So I created the test project with a moving player.
After that I installe VS-code and there I installed the package manager.
With STRG+P and add package I added Npgsql.
VS-Code added the lines:



to my csproj - File.
Now I run dotnet restore in VS-code.
Now I run nuget restore in VS-Code. Sadly, this didn’t worked and I can’t figure out, why.
I removed the lines in csproj-files.

Now I try to open a test-database and write a line in a table for test-reasons:

public override void _Ready(){
    GD.Print("test");
	_screenSize = GetViewport().Size;
	string cs = "Host=kandula.db.elephantsql.com;Username=hidden_user;Password=hidden_pw;Database=hidden_db";
    con = new NpgsqlConnection(cs);
    try
    {
        con.Open();
        NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO public.cars (name, price) VALUES (@n, @p)", con);
        cmd.Parameters.AddWithValue("n", "bla");
        cmd.Parameters.AddWithValue("p", 123);
        //cmd.Prepare();
        cmd.ExecuteNonQuery();
    }
    catch
    {
        GD.Print("catch");
        return;
    }}

First, I don’t know why it doesn’t work with VS-Studio. Also, I don’t know how to handle exceptions in godot.
But: Awesome, it works, the values are in the database. I think I give godot and myself a try and look for some basic tutorials.
Thanks for your help!

Görf | 2020-05-25 17:12

:bust_in_silhouette: Reply From: juppi

It works!

I just added the Npgsql Nuget Package to my Project. Here an Code Example:

using System;
using Npgsql;
using Godot;

public class MainScene : Control

{
public override void _Ready()
{
try
{
NpgsqlConnection conn = new NpgsqlConnection(“Host=localhost;Username=postgres;Password=postgres;Database=postgres”);
conn.Open();
NpgsqlCommand command = new NpgsqlCommand(“SELECT * FROM people”, conn);
NpgsqlDataReader dataReader = command.ExecuteReader();

        while (dataReader.Read())
        {
            GD.Print(dataReader[0], " ", dataReader[1]);
        }
        conn.Close();
    }
    catch (Exception exception)
    {
        GD.Print(exception);
    }
}

}

:bust_in_silhouette: Reply From: joekim7

CHECK THIS OUT:

I was pretty happy to have found this, but then I read the docs that say the client is unstable, and should not be used in production. It also occurred to me that it is likely to become abandonware, and likely incompatible with some future version of PostgreSQL.

I also don’t want to go through the pain of trying to integrate Godot and C#, so integrating PostgreSQL into my game code has become a non-starter.

What I decided on was using JSON files as my lingua-franca in a structured file tree, and having a separate monitor program that reads and writes the database. This way, the files provide real-time data to and from the game servers, and the database lag is immaterial.

stormreaver | 2021-06-13 00:41

Hello, I am the developer of the repository, I am 18 years old and I am in my studies so I did not have time to finish the script. Despite this the project is still relevant and I will be able to resume its development.

The project is still only a draft but I did not know that there could be a real interest in the reference. I have the same need as you so I will continue to maintain it and make it cleaner in the future.

Samuel Marzin | 2021-06-21 21:07

:bust_in_silhouette: Reply From: stormwarestudios

A bit late to the party, but you can quite easily use PostgREST to make calls to your PostgreSQL instance.

I recently put together a demo repository, you can find it here: Peter Kingsbury / godot-4-postgresql-postgrest · GitLab

This is for Godot 4, but the same principles apply for Godot 3 (i.e. use yield in Godot 3, or await in Godot 4).