---
# System prepended metadata

title: VSCode + Docker setup

---

<style>
    .present {
        text-align: left;
    }
</style>


# VSCode + Docker setup
## For M1 issues with Psycopg2 :(

### Initial setup

1. Install Docker ([instructions here](https://docs.docker.com/desktop/mac/install/))
    - Follow the "Mac with Apple Silicon" instruction to install Rosetta2
    - Then click the install button for "Mac with Apple Chip"
2. Open your code in VSCode
3. Install the VSCode extension `Remote - Containers` (by Windows)
4. Return to the `Explore` view to see your files. Click the green button in the bottom left corner:
![](https://i.imgur.com/j4rTco1.png)
5. A menu will pop up with some options. Click `Reopen in Container`:
![](https://i.imgur.com/gtQksOU.png)
6. It will then ask what type of container you want to open. Click `Python 3 & PostgreSQL`.
7. VSCode will now build the container! This might take a few minutes.

### Quick note about virtual environments
If you opened up an existing directory in the container that has a `.venv`, you will need to delete it once you are in the Docker container:
```python=
pipenv --rm
```

Then you can recreate it like so using the `Pipfile` that exists in the directory:
```python=
pipenv install
```

A new `.venv` will be created in a different location in your Docker container. That's ok!

### Setting up the database

Because your postgres Docker container is separate from your local postgres instance, we need to create a database & user within the Docker container.

1. We will need the `CONTAINER ID` for the container running the `postgres:latest` image. This is usually the second container listed. Open an external terminal window and run `docker ps` to see all running containers:
```python=
docker ps
```

2. We are going to enter the container running the `postgres:latest` image. To do so, run this command in the external terminal window:
```python=
docker exec -it <POSTGRES_CONTAINER_ID> psql -U postgres
```
3. You should now be in the `psql` shell within the postgres Docker container. The command prompt should look like so:
```python=
postgres=# 
```
4. Create the user and database as you normally would using the same credentials your app will use:
```python=
create user <user_name> with password '<password>';
create database <db_name> with owner <user_name>;
```
5. This is all we need to do in the postgres Docker container psql shell. You can choose to exit the `psql` shell now or leave it open! You can always return to this shell with the same command to look at your database tables & records:
```python=
docker exec -it <POSTGRES_CONTAINER_ID> psql -U postgres
```

6. Now we need to configure our Python Docker container to access this database. In your VSCode window, open the `.devcontainer/devcontainer.json` file.
7. In the `sqltools.connections` entry under `settings`, change the `database`, `username`, and `password` entries to use the correct credentials for your app:
```python=
"settings": { 
    "sqltools.connections": [{
        "name": "Container database",
        "driver": "PostgreSQL",
        "previewLimit": 50,
        "server": "localhost",
        "port": 5432,
        "database": "YOUR_DB_NAME",
        "username": "YOUR_USER_NAME",
        "password": "YOUR_USER_PASSWOD"
    }],
```

### Leaving the container
To return to your local version of this directory, click the green button in the bottom left corner that says `Dev Container: Python 3 & PostgreSQL @ desktop-linux`.

A menu will pop up. Click the option that says `Reopen Folder Locally`. This will take you out of the Docker container and back to your local version of the directory.

You may need to recreate your `.venv` locally if you previously deleted it in the container:
```python=
pipenv install
```

### You should now be able to interact with your database within the Docker container open in your VSCode window. Happy coding!