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
Project ID:
N/A
N/A
are you good with using postgres?
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.
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 databaseCool. 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?
you'd get all the connection details from the database's service variables, ideally you would use the private url you are given
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
yeah postgres is what I'd recommend, all around great database and uses the lowest base amount of memory when compared to MySQL
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.
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
dashboard looks right?
yep!
would i be trying to connect to postgres.railway.internal or the postgresql://... ?
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
understood. but it looks like i have two private urls
postgres.railway.internal or DATABASE_PRIVATE_URL
how so?
I'm assuming I want the later
well one is just the hostname and one is a url
but just wanted to make sure postgres.railway.internal isn't some magic url
it is kinda magic
it can only be used by the service running on railway
gotcha. yeah. i cant get it to connect when i try with any combination.
try with the public url first
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:postgresql://postgres:[email protected]:52289/railway
is the the correct format for the URL?
yep
or well.
i think!
might need the jvm expert in here
lol
time for a trip to the applicable docs
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.
haha not railways docs this time
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
do you have the necessary driver installed?
stop sending me trains lmao I don't even know java, but thank you
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.
let me know if you make any progress
url looks right though: https://jdbc.postgresql.org/documentation/use/
there is a few spring templates for railway you could possibly check out? I wouldn't know what to look for in them though
yeah. as always. probably just something tiny thats wrong here
trying to declare these drivers /shruggie
GitHub
GitHub - brettwooldridge/HikariCP: ε
HikariCPγ»A solid, high-perform...
ε
HikariCPγ»A solid, high-performance, JDBC connection pool at last. - brettwooldridge/HikariCP
idk wtf hikari is, but apparently its like the goto connection manager for dbs in java land?
I have no clue what that is either
success!
ds.driverClassName = "org.postgresql.Driver"
and had to add another postgres dependency to my build.gradle
awesome glad you could solve!
hell yeah. its workingg!
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
railway auto restarts when your service crashes π
well up to 10 times
very cool that sqldelight just works though. i write sql code. and it generates type safe kotlin code for me. not bad π
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.
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
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"
hell yeah get firebase out of here, railway is for everything!
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.
theres other auth providers that only specialize in auth, i think there may even be some you can self host on railway?
ive worked with auth0 many many many years ago. such a poor experience. but yeah. might be time to learn something new π
I've heard good things about clerk, don't know if they have a java sdk though