R
Railwayβ€’9mo ago
leogtzr

PostgreSQL - read: connection reset by peer

Hello! I have a Golang application that uses PostgreSQL, I am trying to the Go PSQL client to the database using the DATABASE_URL or the DATABASE_PRIVATE_URL but I am not able to connect. I have also tried to "Promote" the env variable but the result is the same, in the backend I get this: panic: read tcp XXX...:59288: read: connection reset by peer What am I missing? Thanks for the help.
27 Replies
Percy
Percyβ€’9mo ago
Project ID: 3e235aa2-f466-4409-b29f-ae3400493059
leogtzr
leogtzrβ€’9mo ago
Project ID: 3e235aa2-f466-4409-b29f-ae3400493059
Brody
Brodyβ€’9mo ago
can you show the go code please?
leogtzr
leogtzrβ€’9mo ago
Yeah, this is my main.go
Brody
Brodyβ€’9mo ago
and this code works locally?
leogtzr
leogtzrβ€’9mo ago
Locally I build the DB using Docker
Brody
Brodyβ€’9mo ago
make your job easier, use github.com/xo/dburl
dsn, err := dburl.Parse(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}

DB, err = sql.Open("postgres", dsn.DSN)
// ...
dsn, err := dburl.Parse(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}

DB, err = sql.Open("postgres", dsn.DSN)
// ...
then theres no need to build the dsn yourself like you do on line 51 of the file you sent me and that also means no need for any other database variable than just DATABASE_URL
leogtzr
leogtzrβ€’9mo ago
Will try that tomorrow morning and let you know Thanks for the help
leogtzr
leogtzrβ€’9mo ago
I am afraid it didn't work 😦 I am wondering if I should use the DATABASE_PRIVATE_URL env variable instead. This is the code I worked: I am aware that it requires some refactor on that specific piece of code, will do that once I get a successful connection, thanks for the help! Seeing this a few times before it crashes: 2023/11/02 16:07:26 Using DSN name=(postgres://postgres:.....@monorail.proxy.rlwy.net:59288/railway) panic: read tcp 172.17.1.228:47558->35.212.181.170:59288: read: connection reset by peer goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:88 +0xa3d
Brody
Brodyβ€’9mo ago
there's no need to use that big if else, please just use the sample code as shown here
leogtzr
leogtzrβ€’9mo ago
I changed it as suggested. However: /app/cmd/insultos/main.go:62 +0x4b6 panic: read tcp 172.17.1.14:54532->35.212.181.170:40569: read: connection reset by peer goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:62 +0x4b6 panic: read tcp 172.17.1.14:51226->35.212.181.170:40569: read: connection reset by peer goroutine 1 [running]: main.main() I am missing a step somewhere
Brody
Brodyβ€’9mo ago
okay i think the code looks fine, now try adding a time.Sleep(3 * time.Second) before the sql.Open statement
leogtzr
leogtzrβ€’9mo ago
Added a .Sleep(10) here: func main() { var err error dsn, err := dburl.Parse(os.Getenv("DATABASE_URL")) if err != nil { panic(err) } log.Println("Waiting...") time.Sleep(10 * time.Second) log.Printf("Done...") DB, err = sql.Open("postgres", dsn.DSN) err = DB.Ping() if err != nil { panic(err) } else { log.Println("DB Connection OK...") } fmt.Printf("debug:x connection=(%s) - (%s)\n", dsn, dsn.DSN) Output: 2023/11/02 23:16:39 Waiting... 2023/11/02 23:16:49 Done... panic: EOF goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:67 +0x5b6 2023/11/02 23:16:51 Waiting... 2023/11/02 23:17:01 Done... panic: read tcp 172.17.1.14:44732->35.212.181.170:40569: read: connection reset by peer goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:67 +0x5b6 2023/11/02 23:17:02 Waiting... 2023/11/02 23:17:12 Done... panic: read tcp 172.17.1.14:45018->35.212.181.170:40569: read: connection reset by peer goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:67 +0x5b6 2023/11/02 23:17:14 Waiting... 2023/11/02 23:17:24 Done... panic: read tcp 172.17.1.14:58472->35.212.181.170:40569: read: connection reset by peer goroutine 1 [running]: main.main() /app/cmd/insultos/main.go:67 +0x5b6 2023/11/02 23:17:25 Waiting... 😦
Brody
Brodyβ€’9mo ago
can you connect to this database from some kind of desktop software like dbgate or pgadmin?
leogtzr
leogtzrβ€’9mo ago
I tried using pgAdmin:
No description
leogtzr
leogtzrβ€’9mo ago
Also, when I try using psql this way: psql 'postgres://postgres:XXXXXXXX@monorail.proxy.rlwy.net:40569/railway' I get: psql: error: connection to server at "monorail.proxy.rlwy.net" (35.212.45.179), port 40569 failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. ~ telnet monorail.proxy.rlwy.net 40569 2>&1 βœ” 2 task 20:12:53 Trying 35.212.45.179... Connected to monorail.proxy.rlwy.net. Escape character is '^]'. Connection closed by foreign host. ~
Brody
Brodyβ€’9mo ago
if I recall correctly the default username is railway?
leogtzr
leogtzrβ€’9mo ago
Both PGUSER and POSTGRES_USER are "postgres" in my config based on the provided environment variables. I can change it to railway and test
Brody
Brodyβ€’9mo ago
no need try restarting your database
leogtzr
leogtzrβ€’9mo ago
Hmmm, I did a few times, no luck. I also restarted the deployment a few times.
Brody
Brodyβ€’9mo ago
why are you using telnet lol use a database client like dbgate
leogtzr
leogtzrβ€’9mo ago
It was just a connectivity test host+port
Brody
Brodyβ€’9mo ago
haha use an actual database client please
leogtzr
leogtzrβ€’9mo ago
I did already
leogtzr
leogtzrβ€’9mo ago
No description
Brody
Brodyβ€’9mo ago
are there any errors in the postgres logs?