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

2 years ago

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?

83 Replies

coltonidle
FREEOP

2 years ago

N/A


2 years ago

are you good with using postgres?


coltonidle
FREEOP

2 years ago

never used postgres. but im open to using it. going to be reallllly basic stuff going in there for right now. 😊


coltonidle
FREEOP

2 years ago

my understanding of postgres is that its just another variant of sql. so anything in that realm of things should be fine.


2 years 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
FREEOP

2 years 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?


2 years 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
FREEOP

2 years 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…


2 years 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
FREEOP

2 years 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.


2 years ago

bingo


2 years ago

as for the how to connect, I wouldn't know, but spring's docs would! 🙂


2 years ago

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
FREEOP

2 years ago

dashboard looks right?

1219046712635818000


2 years ago

yep!


coltonidle
FREEOP

2 years ago

would i be trying to connect to postgres.railway.internal or the postgresql://… ?

1219098701881020400
1219098702149451800


2 years 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
FREEOP

2 years ago

understood. but it looks like i have two private urls


coltonidle
FREEOP

2 years ago

postgres.railway.internal or DATABASEPRIVATEURL


2 years ago

how so?


coltonidle
FREEOP

2 years ago

I'm assuming I want the later


2 years ago

well one is just the hostname and one is a url


coltonidle
FREEOP

2 years ago

but just wanted to make sure postgres.railway.internal isn't some magic url


2 years ago

it is kinda magic


2 years ago

it can only be used by the service running on railway


coltonidle
FREEOP

2 years ago

gotcha. yeah. i cant get it to connect when i try with any combination.


2 years ago

try with the public url first


coltonidle
FREEOP

2 years ago

java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:postgresql://postgres:LONGCODE@viaduct.proxy.rlwy.net:52289/railway


2 years ago

is the the correct format for the URL?


coltonidle
FREEOP

2 years ago

yep


coltonidle
FREEOP

2 years ago

or well.


coltonidle
FREEOP

2 years ago

i think!


coltonidle
FREEOP

2 years ago

1219110999811035100


coltonidle
FREEOP

2 years ago

might need the jvm expert in here


coltonidle
FREEOP

2 years ago

lol


2 years ago

time for a trip to the applicable docs


coltonidle
FREEOP

2 years ago

heyyy i look at the docs 😬


coltonidle
FREEOP

2 years ago

but yeah. that looks about right.


coltonidle
FREEOP

2 years ago

anyway. too tired at this point. going to bed. will try again tomorrow.


2 years ago

haha not railways docs this time


coltonidle
FREEOP

2 years ago

yeah. i looked up sqldelight docs + hikari + sprint boot + ktor. lol


coltonidle
FREEOP

2 years ago

I'm fairly confident in that setup.


coltonidle
FREEOP

2 years ago

but still get
java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:postgresql://postgres:LONGCODE@viaduct.proxy.rlwy.net:52289/railway


coltonidle
FREEOP

2 years ago

even found someone doing sprint boot + railway tutorial on youtube


2 years ago

do you have the necessary driver installed?


2 years ago

stop sending me trains lmao I don't even know java, but thank you


coltonidle
FREEOP

2 years ago

the trains were for the sanity check at the begining of this


coltonidle
FREEOP

2 years ago

trying driver stuff now


coltonidle
FREEOP

2 years ago

idk what it is, but found some talk about it.


2 years ago

let me know if you make any progress


coltonidle
FREEOP

2 years ago


2 years 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
FREEOP

2 years ago

yeah. as always. probably just something tiny thats wrong here


coltonidle
FREEOP

2 years ago

trying to declare these drivers /shruggie



coltonidle
FREEOP

2 years ago

idk wtf hikari is, but apparently its like the goto connection manager for dbs in java land?


2 years ago

I have no clue what that is either


coltonidle
FREEOP

2 years ago

success!


coltonidle
FREEOP

2 years ago

ds.driverClassName = "org.postgresql.Driver"


coltonidle
FREEOP

2 years ago

and had to add another postgres dependency to my build.gradle


coltonidle
FREEOP

2 years ago

Ktor: Hello, Java 17.0.9! and res HockeyPlayer(player_number=10, full_name=Corey Perry)

2 years ago

awesome glad you could solve!


coltonidle
FREEOP

2 years ago

hell yeah. its workingg!


coltonidle
FREEOP

2 years ago

1219132266505179100


coltonidle
FREEOP

2 years ago

server dev is very different from mobile dev. lmao.


coltonidle
FREEOP

2 years ago

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


2 years ago

railway auto restarts when your service crashes 🙂


2 years ago

well up to 10 times


coltonidle
FREEOP

2 years ago

very cool that sqldelight just works though. i write sql code. and it generates type safe kotlin code for me. not bad 👌

1219290300942389200


coltonidle
FREEOP

2 years 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.


2 years 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


2 years ago

notifications for crashed deploys should be enabled by default, but check anyway


coltonidle
FREEOP

2 years ago

thanks!


coltonidle
FREEOP

2 years ago

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.


coltonidle
FREEOP

2 years ago

time to update my resume


coltonidle
FREEOP

2 years ago

"Backend dev with Postgres experience"


2 years ago

hell yeah get firebase out of here, railway is for everything!


coltonidle
FREEOP

2 years 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.


coltonidle
FREEOP

2 years ago

i know a lot of people have been going with supabase as well. might have to see what thats all about.


coltonidle
FREEOP

2 years ago

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.


2 years ago

theres other auth providers that only specialize in auth, i think there may even be some you can self host on railway?


coltonidle
FREEOP

2 years ago

ive worked with auth0 many many many years ago. such a poor experience. but yeah. might be time to learn something new 🙂


2 years ago

I've heard good things about clerk, don't know if they have a java sdk though


Loading...