#PostgreSQL - read: connection reset by peer

38 messages · Page 1 of 1 (latest)

quaint dock
#

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.

long onyxBOT
#

Project ID: 3e235aa2-f466-4409-b29f-ae3400493059

shadow condorBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

quaint dock
#

Project ID: 3e235aa2-f466-4409-b29f-ae3400493059

marsh forge
#

can you show the go code please?

quaint dock
marsh forge
#

and this code works locally?

quaint dock
#

Locally I build the DB using Docker

marsh forge
#

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)
// ...
#

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

quaint dock
#

Will try that tomorrow morning and let you know

#

Thanks for the help

marsh forge
#

no problem!

quaint dock
#

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:[email protected]: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

marsh forge
quaint dock
#

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

marsh forge
#

okay i think the code looks fine, now try adding a time.Sleep(3 * time.Second) before the sql.Open statement

quaint dock
#

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

😦

marsh forge
#

can you connect to this database from some kind of desktop software like dbgate or pgadmin?

quaint dock
#

I tried using pgAdmin:

#

Also, when I try using psql this way:

psql 'postgres://postgres:[email protected]: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.

marsh forge
#

if I recall correctly the default username is railway?

quaint dock
#

Both PGUSER and POSTGRES_USER are "postgres" in my config based on the provided environment variables.

#

I can change it to railway and test

marsh forge
#

no need

#

try restarting your database

quaint dock
#

Hmmm, I did a few times, no luck.

#

I also restarted the deployment a few times.

marsh forge
#

why are you using telnet lol

#

use a database client like dbgate

quaint dock
#

It was just a connectivity test

#

host+port

marsh forge
#

haha use an actual database client please

quaint dock