Self-hosted connection to db?
I'm struggling with how to connect to my DB after setting up the self-hosted version.
Anyone been through the same, or can guide one in the right direction on this? Fairly lost currently 🙃
129 Replies
Are you using docker-compose or supabase cli to run self hosted instance?
Also, when you say you want to connect to db do you mean via some client like pgadmin/dbeaver or from your app?
docker-compose setup yes, and correct, running the connection from a next.js project, with prisma
Sorry for hijacking this, but does supabase-cli even support connecting to the self hosted instance? it suggests me to link a remote supabase project to run migrations
You just use the
--db-url
flag when running most database related commands from the CLI. It's in the CLI docs.or use
--local
flag. cli will automatically use the local db. But this is only for containers started with cliFrom the guide: https://supabase.com/docs/guides/self-hosting/docker
This connections tring is referenced:
However this format doesn't seem to work for me
Self-Hosting with Docker | Supabase Docs
Learn how to configure and deploy Supabase with Docker.
Can you share the error you get
>Error: P1001: Can't reach database server at
and then my ip address, where the self-hosted is running
Try this url. As it is. just add your own password and database.
postgres://postgres.your-tenant-id:[POSTGRES_PASSWORD]@localhost:5432/[POSTGRES_DB]
database by default is postgresSame, > Error: P1001: Can't reach database server at
localhost:5432
have you verified if all the containers are healthy?
But this shouldn't work either as I understand. As I'm trying to connect outside from outside the server itself?
Yes

If you're using the default docker-compose.yml in supabase repo, supavisor is exposed on port 5432
You can run this cmd and verify
supabase-pooler
service's port
Looks correct

ya, so port 5432 is published on host so localhost:5432 should work
Also when running on an external server, and running the next.js project locally?
you mean running supabase self hosted on external server?
Yes
the hostname and port will have to be updated.
that totally depends on you how you deploy the instance. Whether you want to make the database accessible on the internet or not
Is has to be accessible, as it's a public forum format the DB controls
Where to change the hostname and port, and to what in this case? 😅 hostname = server ip?
correct, in the url localhost is hostname.
you can use ip or a domain you've setup
what about port?
you'll have to open 5432 in your server firewall
Are you going to deploy nextjs app and supabase on same server?
6543 better for external?
No
It doesn't matter
Whatever port you use you'll have to update it in docker-compose and your firewall
Got it
Thanks for the help, I'll give it a try 😊
localhost still not working?
nope
let me run it locally. will get back to you
Works fine for me


You can actually connect to the db aswell? and push/pull data?

hmmm
I don't use prisma. The schema file is just an example I used off the internet. As you were getting connection errors, that is my main concern
What is your use case? Are you going to run any migrations with prisma?
In prisma docs, it is mentioned that if you want to run migrations, only then prisma needs a direct connection to db. Otherwise you should use connection pooler
https://www.prisma.io/docs/orm/overview/databases/supabase#specific-considerations
But the connection pooler would require the same setup
But yes, migrations are needed
Got the same error, but retried and it succeeded. Also, added a direct url as mentioned in docs



I'm using this example: https://github.com/prisma/nextjs-prisma-postgres-demo
hmm, just testing with my server currently
But cool to see you got it working, has to be some server settings then
supavisor service's ports are already exposed on 5432 and 6543 on your machine as well as you sent the picture above. You can try pinging the port
Assume you've deployed to this an external server instead of running locally, wouldn't you just replace the localhost with the server IP?
Yes, I mentioned it above
Just double checking
How did you get rid of that port :8000 from the dashboard URL you send in your example?
Currently i have to visit it on {server_ip}:8000
Can you tag the example you're referring to
https://discord.com/channels/839993398554656828/1384131143644876831/1384144049572937760
i can see your dashboard url is just suapbase.example.com
Mine is fixed to :8000 port
Oh yeah, that's just I edited the hosts file and I'm proxying via caddy to supabase
I'm listening on port 443
hosts file? Where are these
the docker-compose.yml setup file?
This is a special file on an OS which is used for DNS resolution.
1. First the cache is checked for a new domain, if a mapping is found for a hostname, then that ip address is used.
2. Then, this hosts file is checked, if hostname is found then the ip address added in this file is found.
This is how localhost is looped back. It is added in hosts file and mapped to 127.0.0.1
Are you on windows or linux?
linux (talking the external server here)
If you want to test this locally, then you edit hosts file on your local system
For server, you setup a domain and a reverse proxy
If you're looking for an automated solution https://github.com/singh-inder/supabase-automated-self-host
This is what I use locally and on servers
if your local system is linux as well I have a guide to setup hosts file https://github.com/singh-inder/supabase-automated-self-host/discussions/6
This guide will help for setting up https for kong only
https://towardsdev.com/self-hosted-supabase-with-lets-encrypt-certificate-for-kong-2a6a8dd298db
hmm, were just wondering whether the :8000 port did something with the self-hosted postgres url
no, it shouldn't. kong is listening on this port. It shouldn't affect supavisor which is listening on ports 5432 and 6543
Thes really should work then
DATABASE_URL="postgres://postgres.your-tenant-id:${DATABASE_PASSWORD}@${DATABASE_SERVER_IP}:6543/postgres" DIRECT_URL="postgres://postgres.your-tenant-id:${DATABASE_PASSWORD}@${DATABASE_SERVER_IP}:5432/postgres"
Did you open the ports from firewall?
Yea
also in the cloud provider's firewall?
if you're using aws ec2, you'll have to open ports in security group as well. vpc will have to opened if not using default vpc
This is on Hetzner

I've never used hetzner. Does this also apply to outgoing?
Woops, meant to set as outbound, applied them wrong
no both has to be done
oh
Still the same, can't access it
let me try it on aws ec2. will get back to you.
you're trying to connect via prisma, right?
yes
works fine for me. This is my url
You can try

what, how 😄
Give me a second
this is the schema.prism
I'm running a spot instance, aws can terminate these instances at any time so try it as soon as possible

what command do you run?
migrate doesn't work either


I'm running prisma client v.6.9.0
Can you try and upgrade?

Are you behind some firewall which could be blocking some ip addresses?
🤔
Shouldn't be no
I can connect to my external server via ssh, and no firewall blocking anything there
try with the same example I'm using. maybe something is wrong with your prisma setup (highly doubtful) but still you can try
https://github.com/prisma/nextjs-prisma-postgres-demo
Works with these settings in
schema.prisma
I only have this for generator client block in schema.prism
Can you try the above? Not that I think it changes anything though
wait

What is going on man 😂
Try with the hostname
replace ip with
supabase.servehttp.com
port will remainSame issue

Can you setup another server and copy your prisma setup in that.
I think this has something to do with your network
You can try making a prisma push request from this another server
or use your mobile network for internet
use your mobile's internet on your pc and try making request. If this doesn't work then try with another server
Hey just checking in, are you going to try to make a request to my instance or should I take it down?
No just take it down
Obviously something wrong in my end
@inder you installed it 1:1 as this setup on the aws server?
https://supabase.com/docs/guides/self-hosting/docker
Self-Hosting with Docker | Supabase Docs
Learn how to configure and deploy Supabase with Docker.
I use supabase-automated-self-host. it simply adds a proxy in front of kong for dashboard, rest api, realtime and storage. Doesn't modify the default setup.
When connecting db, it connects directly to published ports on the server.
weird, just had a suspision on the docker-compose setup, if something were missing in that guide
Did you try setting up another server?
Not yet, will try next
To rule out any issues with prisma, try connecting to db with a client with psql or pgadmin(use direct url here). Also try with mobile internet. Could be an issue with your ISP
Before that make sure the ports on which you're connecting on your server are accessible from internet https://portchecker.io/
tried mobile
local docker works as expected
trying with a new server now
before that check if you can access port 5432 on the server you've deployed instance
port is all good
Got another workbuddy to test it, he can migrate all fine to the server i'm trying to do it to. So not quite sure what is going on
Then its definitely your ISP
@inder You've already been such a great help so far, can I maybe get you to try and follow this guide 1:1 https://supabase.com/docs/guides/self-hosting/docker
on a AWS spot instance?
Really need to ensure the github repo that is cloned isn't somewhat buggy with the setup in ports or ips allowed.
My coworker can no longer access the db as well, it's so weird
Let me setup an instance. Will ping you
here you go
works on my end

This is so weird

And it even says it's open

Login via browser to dashboard works just fine
But does it on my own instance as well
Do you have some db client like pgadmin or dbeaver?
Only the prisma commands that doesn't work
No
psql?
Yea
try logging in with psql
psql connection works fine too
tried it with my own as well
can you run migrations with supabase cli?
it can also take --db-url flag
or is prisma migrations a requirement?
try running migrations once with supabase cli
Seems to work with the Supabase cli
You tried on your own db? didn't update in my instance
Or maybe not, it says it updated the remote instance, but nothing changed.
Tried on my own external server
can you ask your friend who was able to run migrations successfully yesterday
Will have to try tomorrow, he is out of office
And you tried with another server?
what command did you run here?
npx supabase db push --db-url ****
?
Yes
Same issue
Only works locally
Not working even from on a separate server. I was going to ask you that I will try pushing migrations from my end but you mentioned that yesterday your friend was able to run migrations
Yea... this is a real head scratcher
But once again, much appreciate your help 🙏
If this is a disposable instance, I can try running from my end if you want.
you can always reset the instance by deleting
volumes/db/data
directoryNo need, this eliminated on of my main concerns
Just wanted to ask, when you ran from another server did you allow inbound & outbound connections on this server? I don't know the default firewall settings of hetzner
Yes I did
But doesn't seem to affect anything, since I can connect fine without inbound and outbound port settings, via psql
I am out of ideas on this one. If psql can connect, can't think of any reason why prisma can't
I'm stumped too, will reach out once finding a solution.
What... If I run
prisma migrate dev
works fine, it's the npx prisma migrate dev
that fails, even though they are the same version installedis there any antivirus on your system which could be blocking DNS queries for processes running with npx?
Have to dig deeper into this