Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose postgresql so that it can be accessed outside of Docker #4269

Closed
Pomax opened this issue Mar 2, 2020 · 4 comments · Fixed by #4294
Closed

expose postgresql so that it can be accessed outside of Docker #4269

Pomax opened this issue Mar 2, 2020 · 4 comments · Fixed by #4294
Assignees
Milestone

Comments

@Pomax
Copy link
Contributor

Pomax commented Mar 2, 2020

This is especially important for database issues relating to real data, such as deleting user accounts from the prod/staging instances.

@Pomax Pomax added this to the Mar 9 milestone Mar 2, 2020
@Pomax Pomax self-assigned this Mar 2, 2020
@cadecairos
Copy link

The root of the problem here is that we're defaulting to using SQLite.

We need to update a few things. Firstly, we need to update docker-compose.yml:

In the postgres service, update ports to be: `"5432:5431" (changes the port exposed on the host in case it's already running a postgres DB). In the same service, add:

environment:
  - POSTGRES_USER=foundationsite
  - POSTGRES_PASSWORD=devmode
  - POSTGRES_DB=foundationsite

This will, upon DB creation, add foundationsite as a super user, set their password to devmode and create a default database named foundationsite. This will allow us to connect to the database without needing to use trust authentication, which is the default.

We'll need to update the default connection string in env.default to be postgres://foundationsite:devmode@localhost:5431/foundationsite. There might be some other places we need to set this, but I don't think we need to do anything to support this change on our CI, since it's not using Docker.

@cadecairos
Copy link

cadecairos commented Mar 2, 2020

helpful reference: https://hub.docker.com/_/postgres

Scroll down to "Environment Variables"

@Pomax
Copy link
Contributor Author

Pomax commented Mar 2, 2020

turns out the ports are host:docker, rather than docker:host. Things now work except that postgres does not know the 'foundationsite' role, so I'm simply going to make this postgres for db, user, and database and see if that works.

@Pomax
Copy link
Contributor Author

Pomax commented Mar 4, 2020

PR filed, it changes tasks.py a little, checking the docker-compose.yml for creds, and if found, templates those in:

    with open(env_file, 'r') as f:
        env_vars = f.read()

    # We need to strip the quotes because Docker-compose considers them as part of the env value.
    env_vars = env_vars.replace('"', '')

    # We also need to make sure to use the correct db values based on our docker settings.
    username = password = dbname = 'postgres'
    with open('docker-compose.yml', 'r') as d:
        docker_compose = d.read()
        username = re.search('POSTGRES_USER=(.*)', docker_compose).group(1) or username
        password = re.search('POSTGRES_PASSWORD=(.*)', docker_compose).group(1) or password
        dbname = re.search('POSTGRES_DB=(.*)', docker_compose).group(1) or dbname

    # Update the DATABASE_URL env
    new_db_url = f"DATABASE_URL=postgresql://{username}:{password}@postgres:5432/{dbname}"
    old_db_url = re.search('DATABASE_URL=.*', env_vars)
    env_vars = env_vars.replace(old_db_url.group(0), new_db_url)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants