# fly deploy fails to connect to Postgres - wrong port?

**URL:** <https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186>\
**Category:** General\
**Tags:** postgres, rails, login-only\
**Created:** [December 9, 2023, 1:26am UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186 "2023-12-09T01:26:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leo-frdm](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@leo-frdm](https://community.fly.io/u/leo-frdm)\
**Post date:** [December 9, 2023, 1:26am UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/1 "2023-12-09T01:26:13Z")

</div>

Hi,

fly deploy is failing on my Rails app due to not being able to reach postgres, at a point of docker build when activerecord tries to establish a connection I get this:

```auto
#0 5.225 /rails/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:87:in `rescue in new_client': connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (ActiveRecord::ConnectionNotEstablished)
#0 5.225 Is the server running locally and accepting connections on that socket?

```

I can connect to the db with pg connect ( fly pg connect --app frdm-production). Curiously, postgres seems to be using a different port:

```auto
psql (15.3 (Debian 15.3-1.pgdg120+1))
Type "help" for help.

postgres=# SELECT name, setting
FROM pg_settings
WHERE name = 'port';
 name | setting 
------+---------
 port | 5433
(1 row)

```

these are the apps, created with the cli wizard:

```auto
fly apps list
NAME OWNER STATUS PLATFORM	LATEST DEPLOY 
fly-builder-red-surf-271	personal	deployed	machines	             	
frdm personal	pending machines	             	
frdm-production personal	deployed	machines	

```

is 5433 just the internal port that at some point in fly’s internal docker-compose equivalent gets mapped to the right port, 5432? Or is this a legit problem that is causing the connection to fail?

---

<div class="post-metadata">

**Author:** ![rubys](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/rubys/32/16907_2.png) [@rubys](https://community.fly.io/u/rubys)\
**Post date:** [December 9, 2023, 1:37am UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/2 "2023-12-09T01:37:01Z")

</div>

> [@leo-frdm](#):
>
> `on socket "/var/run/postgresql`

What this generally means is that the `DATABASE_URL` secret isn’t set. The easiest/best way to set this secret is to use the [fly postgres attach](https://fly.io/docs/flyctl/postgres-attach/) command.

---

<div class="post-metadata">

**Author:** ![leo-frdm](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@leo-frdm](https://community.fly.io/u/leo-frdm)\
**Post date:** [December 11, 2023, 5:43pm UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/3 "2023-12-11T17:43:18Z")

</div>

DATABASE\_URL was set. I did `fly detach` and `fly attach` just to make sure the DATABASE\_URL value is correct, and got the same error

---

<div class="post-metadata">

**Author:** ![rubys](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/rubys/32/16907_2.png) [@rubys](https://community.fly.io/u/rubys)\
**Post date:** [December 11, 2023, 6:08pm UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/4 "2023-12-11T18:08:15Z")

</div>

If database URL is set, then the next most likely culprit is your `config/database.yml`. For information on how Rails resolves the port, see [Configuring Rails Applications — Ruby on Rails Guides](https://edgeguides.rubyonrails.org/configuring.html#connection-preference)

---

<div class="post-metadata">

**Author:** ![leo-frdm](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@leo-frdm](https://community.fly.io/u/leo-frdm)\
**Post date:** [December 11, 2023, 6:23pm UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/5 "2023-12-11T18:23:32Z")

</div>

Yep, that was it. I thought `DATABASE_URL` took precedence over and replaced database.yml, but rather they are merged. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/fly1/original/3X/6/e/6e8955d00cb218e186ebbe3a6ab06fefdce15290.svg) [@system](https://community.fly.io/u/system)\
**Post date:** [December 18, 2023, 6:24pm UTC](https://community.fly.io/t/fly-deploy-fails-to-connect-to-postgres-wrong-port/17186/6 "2023-12-18T18:24:30Z")

</div>

Closed on December 18, 2023, 6:24pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
