Skip to content
Advertisement

Can’t connect to my Docker Postgres from python suddenly

I’ve been trying to configure my m1 to work with an older ruby on rails api and I think in the process I’ve broken my ability to connect any of my python apis to their database images in docker running locally.

When I run:

psql -U dev -h localhost database

Instead of the lovely psql blinking cursor allowing me to run any sql statement I’d like I get this error message instaad:

psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  database "dev" does not exist

I’ve tried docker-compuse up and down and force recreating and brew uninstalling postgres and reinstalling postgres via brew. I’ve downloaded the postgres.app dmg and made sure to change it to a different port hoping that that would trigger the steps needed just for psycopg2 to connect to the docker image.

the docker-compose.yaml looks like this:

services:
  db:
    image: REDACTED
    container_name: db_name
    restart: always
    environment:
      POSTGRES_USER: dev
      POSTGRES_HOST_AUTH_METHOD: trust
    networks:
      default:
        aliases:
          - postgres
    ports:
    - 5432:5432

What am I missing and what can I blame ruby on rails for (which works by the way) 🤣

Advertisement

Answer

So the answer is pretty simple. What was happening was that I had a third instance of postgres running on my computer that I had not accounted for which was the brew version. Simply running brew services stop postgres and later brew uninstall postgres fixed all my problems with being able to have my ruby on rails api work which rely on “postgres native” on my mac (protip, I changed this one to use port 5431) and my python api work which use a containerized postgres on port 5432 without any headaches. During some intial confusion during my Ruby on Rails setup which required me getting Ruby 2.6.7 running on an m1 mac I must have installed postgres via brew in an attempt to get something like db:create to work.

4 People found this is helpful
Advertisement