I use npgsql extensively in C# server client work but it doesn’t seem to work in Unity. I used the nuget asset to install it with nuget and I just get a bunch of errors that prevent me from doing anything until I completely uninstall everything that was installed for npgsql. I find it hard to believe that it cannot be used at all but I have no seen any forum posts providing any confirmed solution. Most questions on it seem to be left with zero answers. There has to be a solution to using postgresql in Unity?
It’s less a matter of using postgresql, and more a matter of using a postgresql api library that is supported by Unity.
The library you mention, npgsql, is a library that is just a dataprovider that links to postgresql. It’s not postgresql itself.
With that clarification put foward, what follows applies to basically any 3rd party library you want to use in Unity…
How to get a library not necessarily designed for Unity to work in Unity requires that the library targets a version of .Net that Unity supports, and that all dependencies also fit into that, and also that all of those dependencies are included.
Note that nuget won’t necessarily force include dependencies that are part of the .net suite, but rather only other nuget packages. It just assumes that you’re in a standard Visual Studio setting where built in dependencies can self resolve through your GAC. BUT… in Unity this isn’t necessarily true. While Unity technically supports using Visual Studio for editing, it actually compiles everything itself, has its own specialized .net/mono runtime, and since it’s cross-platform doesn’t include all of the same dependencies that Visual Studio generally has by default.
Knowing if a specific library is supported in Unity is basically a case by case scenario. Popular ones like say Newtonsoft JSON.Net will often have forked/ported versions with everything working in Unity. While less popular ones kind of just need someone like yourself to resolve it and hopefully share it publicly.
With that said… Unity has for a while now been slowly chugging towards larger and larger support for .Net. Hopefully in the nearish future they roll out full “.Net Core” support (the Microsoft standard for true cross-platform .Net), at which point libraries designed for .Net Core should mostly work with little issue (again version requirements). But, they’re not quite there.
…
WIth that said… since npgsql is an implementation of a dataprovider for postgresql, I’m willing to bet that one of the major dependencies its missing is System.Data.dll which Unity does not have. You should be able to copy that from your .net’s install into your Unity project and have it work (at the very least, work for windows builds). But there’s possibly other dependencies as well…
…
And lastly, if you don’t mind me asking… what is your need for directly accessing a db from your game?
Usually the places you might need a database are like on a server. Rather than in a game. And putting any db just rawdog out on the internet for games to access is sort of a giant security risk.
Or is this a situation where you just have large amounts of gameplay data that would be better organized in a database than in say a common save file format like json or something?
Or is this like a large multiplayer game where the server is also written in Unity, and the server needs a db to store data about every connecting client?
This may be not what you are aiming for but in case you need the database for storing large amounts of local data in a structured way to run queries for statistical reasons (how many enemies with a blue helmet has the player killed in the first 10 minutes?) then you might take a look at SQLite, I used it a while ago with a plugin from the asset store and it was nice to work with. The db is just a single file you can inspect with tools outside of Unity.
I don’t remember which asset I used but here is an example: SimpleSQL | Integration | Unity Asset Store
I appreciate the elaboration. It’s for mapping a ton of data generated outside of Unity that is organized into a “shape” stored in a remote postgres server with the intention of bringing it into unity quickly via an editor script and making sense of it. It would not be used in game, but the data queried from it would be. The server is remote, I would be querying it from npgsql.
I’ll probably look for an older version maybe, but like you said, and I ran into a ton of problems with, is all the dependencies. So I’ll probably just map them in a console app and then bring them into unity without npgsql. If you were going to suggest something in your great wisdom. I’m all ears and would appreciate it.