R
Railwayβ€’9mo ago
coltonidle

New to backend dev, what's the TLDR for adding a database to my jvm backend hosted on railway?

New to backend dev (android/kotlin dev originally) and I'm using ktor to handle some basic incoming routes at the moment. Easy stuff. I want to now be able to take an incoming route POST request and insert it into a basic sql/sqlite database (not really married to what db. ive used sqlite on android because it comes out of the box). What's the tldr for doing that on railway?
56 Replies
Percy
Percyβ€’9mo ago
Project ID: N/A
coltonidle
coltonidleOPβ€’9mo ago
N/A
Brody
Brodyβ€’9mo ago
are you good with using postgres?
coltonidle
coltonidleOPβ€’9mo ago
never used postgres. but im open to using it. going to be reallllly basic stuff going in there for right now. 😊 my understanding of postgres is that its just another variant of sql. so anything in that realm of things should be fine.
Brody
Brodyβ€’9mo ago
click the + New button at the top right and deploy your postgres database, then reference spring's docs on how you would get spring to connect to such a database
coltonidle
coltonidleOPβ€’9mo ago
Cool. So (sorry for the noob question). But essentially is connecting to a db from my server like, just giving it an ip of the db?
Brody
Brodyβ€’9mo ago
you'd get all the connection details from the database's service variables, ideally you would use the private url you are given
coltonidle
coltonidleOPβ€’9mo ago
lemme check it out. a nice sql library in kotlin land is SQLDelight. type safe, generated kotlin from sql queries. https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql/ and i see it supports postgres. so that'd be legit...
Getting Started - SQLDelight
SQLDelight - Generates typesafe Kotlin APIs from SQL
Brody
Brodyβ€’9mo ago
yeah postgres is what I'd recommend, all around great database and uses the lowest base amount of memory when compared to MySQL
coltonidle
coltonidleOPβ€’9mo ago
cool. okay. so its basically as "complicated" as I thought it'd be. 1. create db/postgres on railway 2. then "connect" to it via private address railway gives me.
Brody
Brodyβ€’9mo ago
bingo as for the how to connect, I wouldn't know, but spring's docs would! πŸ™‚ maybe there's a template that comes with a postgres database and you could reference it's source code to see how they have setup the connection
coltonidle
coltonidleOPβ€’9mo ago
dashboard looks right?
No description
Brody
Brodyβ€’9mo ago
yep!
coltonidle
coltonidleOPβ€’9mo ago
would i be trying to connect to postgres.railway.internal or the postgresql://... ?
No description
No description
Brody
Brodyβ€’9mo ago
when your app runs on railway you'd want to use the private url, when it's not running on railway you want to use the public url
coltonidle
coltonidleOPβ€’9mo ago
understood. but it looks like i have two private urls postgres.railway.internal or DATABASE_PRIVATE_URL
Brody
Brodyβ€’9mo ago
how so?
coltonidle
coltonidleOPβ€’9mo ago
I'm assuming I want the later
Brody
Brodyβ€’9mo ago
well one is just the hostname and one is a url
coltonidle
coltonidleOPβ€’9mo ago
but just wanted to make sure postgres.railway.internal isn't some magic url
Brody
Brodyβ€’9mo ago
it is kinda magic it can only be used by the service running on railway
coltonidle
coltonidleOPβ€’9mo ago
gotcha. yeah. i cant get it to connect when i try with any combination.
Brody
Brodyβ€’9mo ago
try with the public url first
coltonidle
coltonidleOPβ€’9mo ago
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:postgresql://postgres:[email protected]:52289/railway
Brody
Brodyβ€’9mo ago
is the the correct format for the URL?
coltonidle
coltonidleOPβ€’9mo ago
yep or well. i think!
coltonidle
coltonidleOPβ€’9mo ago
No description
coltonidle
coltonidleOPβ€’9mo ago
might need the jvm expert in here lol
Brody
Brodyβ€’9mo ago
time for a trip to the applicable docs
coltonidle
coltonidleOPβ€’9mo ago
heyyy i look at the docs 😬 but yeah. that looks about right. anyway. too tired at this point. going to bed. will try again tomorrow.
Brody
Brodyβ€’9mo ago
haha not railways docs this time
coltonidle
coltonidleOPβ€’9mo ago
yeah. i looked up sqldelight docs + hikari + sprint boot + ktor. lol I'm fairly confident in that setup. but still get java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:postgresql://postgres:[email protected]:52289/railway even found someone doing sprint boot + railway tutorial on youtube
Brody
Brodyβ€’9mo ago
do you have the necessary driver installed? stop sending me trains lmao I don't even know java, but thank you
coltonidle
coltonidleOPβ€’9mo ago
the trains were for the sanity check at the begining of this trying driver stuff now idk what it is, but found some talk about it.
Brody
Brodyβ€’9mo ago
let me know if you make any progress
coltonidle
coltonidleOPβ€’9mo ago
Brody
Brodyβ€’9mo ago
there is a few spring templates for railway you could possibly check out? I wouldn't know what to look for in them though
coltonidle
coltonidleOPβ€’9mo ago
yeah. as always. probably just something tiny thats wrong here trying to declare these drivers /shruggie
coltonidle
coltonidleOPβ€’9mo ago
coltonidle
coltonidleOPβ€’9mo ago
idk wtf hikari is, but apparently its like the goto connection manager for dbs in java land?
Brody
Brodyβ€’9mo ago
I have no clue what that is either
coltonidle
coltonidleOPβ€’9mo ago
success! ds.driverClassName = "org.postgresql.Driver" and had to add another postgres dependency to my build.gradle
Ktor: Hello, Java 17.0.9! and res HockeyPlayer(player_number=10, full_name=Corey Perry)
Ktor: Hello, Java 17.0.9! and res HockeyPlayer(player_number=10, full_name=Corey Perry)
Brody
Brodyβ€’9mo ago
awesome glad you could solve!
coltonidle
coltonidleOPβ€’9mo ago
hell yeah. its workingg!
coltonidle
coltonidleOPβ€’9mo ago
No description
coltonidle
coltonidleOPβ€’9mo ago
server dev is very different from mobile dev. lmao. crashes on mobile. not that bad. user can reopen app. on server though. one wrong move. and the entire server is down waiting for me to restart it. lol
Brody
Brodyβ€’9mo ago
railway auto restarts when your service crashes πŸ™‚ well up to 10 times
coltonidle
coltonidleOPβ€’9mo ago
very cool that sqldelight just works though. i write sql code. and it generates type safe kotlin code for me. not bad πŸ‘Œ
No description
coltonidle
coltonidleOPβ€’9mo ago
no way. auto restarts? sick. i wonder if theres a way to get an alert when things go down, can't restart, etc. I assume that's going into the world of observability.
Brody
Brodyβ€’9mo ago
railway doesn't sent an alert for the auto restart, but they will send you an alert when you have exhausted the auto restarts and your container does the final crash notifications for crashed deploys should be enabled by default, but check anyway https://railway.app/account
coltonidle
coltonidleOPβ€’9mo ago
thanks! this is so cool though! I might not have to use firebase anymore as my first option for some of my little android/ios side projects. time to update my resume "Backend dev with Postgres experience"
Brody
Brodyβ€’9mo ago
hell yeah get firebase out of here, railway is for everything!
coltonidle
coltonidleOPβ€’9mo ago
ill still give firebase the leg up when it comes to setting up auth though. super easy to get like 2-3 "login with XYZ" setup. really nice. i know a lot of people have been going with supabase as well. might have to see what thats all about. but in this case. i actually have an app im trying to ship thats just like dead simple, relaying api calls to a 3rd party. but i need to store those "requests" somewhere. so a db was needed.
Brody
Brodyβ€’9mo ago
theres other auth providers that only specialize in auth, i think there may even be some you can self host on railway?
coltonidle
coltonidleOPβ€’9mo ago
ive worked with auth0 many many many years ago. such a poor experience. but yeah. might be time to learn something new πŸ™‚
Brody
Brodyβ€’9mo ago
I've heard good things about clerk, don't know if they have a java sdk though
Want results from more Discord servers?
Add your server