Django connection with postgreSQL

Hello guys! I’m trying to connect django with my postgres app but i’m having some problems.

I’ve generated the DATABASE_URL like this:

flyctl pg attach ac7-postgres
Checking for existing attachments
Registering attachment
Creating database
Creating user

Postgres cluster ac7-postgres is now attached to ac7
The following secret was added to ac7:
DATABASE_URL=postgres://ac7:<‘PASSWORD’>@ac7-postgres.flycast:5432/ac7?sslmode=disable

So I have this code in settings.py:

os.environ['DATABASE_URL'] = 'postgres://ac7:<PASSWORD>@ac7-postgres.flycast:5432/ac7?sslmode=disable'

DATABASES = {
    'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}

but when I try “py manage.py makemigrations” it shows:

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xe3 in position 76: invalid continuation byte

Hi… What happens if you try the more basic fly pg connect -a ac7-postgres?


Aside: You typically don’t want to paste the DATABASE_URL into settings files, since it contains the password.

I can connect with fly pg connect -a ac7-postgres, but can I just leave the settings ‘DATABASES’ empty? What should I code there?

Glad that part worked for you… My aside was referring to the top line of your quote, the one beginning with os.environ['DATABASE_URL'] = ...; you instead want to leave that environment variable unmodified. (Fly will populate it from its secrets vault.)

I don’t use Django myself, but the extensive Django to Production blog post from last year shows the following in settings.py:

DATABASES = {
  # read os.environ['DATABASE_URL']
  'default': env.db()
}

Hope this helps a little!