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
Project ID:
3e235aa2-f466-4409-b29f-ae3400493059
Project ID: 3e235aa2-f466-4409-b29f-ae3400493059
can you show the go code please?
and this code works locally?
Locally I build the DB using Docker
make your job easier, use
github.com/xo/dburl
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
Will try that tomorrow morning and let you know
Thanks for the help
no problem!
heres some real sample code too
https://github.com/brody192/database-connection-tester/blob/main/internal/dbtest/databases/sql.go#L16-L30
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
there's no need to use that big if else, please just use the sample code as shown here
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
okay i think the code looks fine, now try adding a time.Sleep(3 * time.Second) before the sql.Open statement
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...
π¦can you connect to this database from some kind of desktop software like dbgate or pgadmin?
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.
~ 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.
~
if I recall correctly the default username is railway?
Both PGUSER and POSTGRES_USER are "postgres" in my config based on the provided environment variables.
I can change it to railway and test
no need
try restarting your database
Hmmm, I did a few times, no luck.
I also restarted the deployment a few times.
why are you using telnet lol
use a database client like dbgate
It was just a connectivity test
host+port
haha use an actual database client please
I did already
are there any errors in the postgres logs?