Tutorial: Connect a Shiny R App to a PostgreSQL Database

Hey everyone,

I just wrote up a blog post about connecting Shiny R apps to PostgreSQL databases. It's called "Connect Shiny-R App to a PostgreSQL Database" if you want to check it out.

I know a lot of us work with Shiny, and I thought it might be useful to share how to hook it up to a more robust database system. The post goes through the basic setup, some code examples, and a few tips I've picked up along the way.

If you've been curious about using PostgreSQL with your Shiny apps or just want to see how it's done, give it a read. It's not too technical, so don't worry if you're new to databases.

Let me know what you think if you end up reading it. Always open to feedback or questions.

2 Likes

Nice tutorial @edub - I love the gentle introduction to database interactions.

This approach stores the database credentials in plain text in the app code, which seems dangerous. What's the best approach to authenticating the database connection securely, without storing the credentials in the project?

1 Like

How you do this depends on where you are deploying from. For example, GitHub has its secrets functionality, and many other deployment platforms have similar functionality for setting environment variables.

Appsilon has also written a good post about this for R specifically: Appsilon/rhino: vignettes/how-to/manage-secrets-and-environments.Rmd

Best,
Randy

1 Like

yeah I agree with that @randyzwitch said, your db credentials should use whatever secrets feature the platform you're using offers

1 Like

@randyzwitch - thanks for the quick responses!

I'm deploying from Workbench to Connect, and auth was as simple as storing the database service account credentials via the Environment Variables UI in Connect. Is that a reasonable/safe approach? I'm a little embarrassed this functionality was sitting in plain sight :melting_face:

Yup, that's what its there for! Don't be embarrassed, now that you know to look for secrets or environmental variables, you'll see them everywhere going forward

Best,
Randy