Is it possible to connect to the database directly from a separate app?

While possible, the other app can acquire locks on tables and block Discourse from working as usual.

Either go for a new plugin that adds the necessary API endpoints you need or go fully into it by creating another PostgreSQL instance that replicates you Discourse and where you can plug your app.