Our docker-compose runs with version 3.6 and creates a new service called postgres, our new container will be called testpg.
Again, we are going to use an already existing database. In my case that database is called web_db, the postgres user is postgres and so is the password of that user (make sure to use the credentials of your database!). For our new container to be able to connect to that database we specify these variables as environment variables.
We use the lastest postgres image and open port 5432 (default postgres port) in case other, future services might want to connect to this db.
Now to the crucial part: we specify an external volume pgdata that will be mounted into the container at /var/lib/postgres/data. Why exactly this path? Because this PostgreSQL's default database location on Linux. This might be slightly different from version to version, so make sure to specify the correct path here (e.g. in some other project of mine it happend to be /var/lib/postgresql/9.5/main).
Next we need to provide some further information regarding the pgdata Volume. On the same height as services: we set up a new block volumes: where we specify the name of our new volume pgdata (this step is optional however make sure it is the same name specified in the postgres service!)
If we would run docker-compose now Docker would create a completely new volume. However we want to use an existing volume therefore we need to set external: true and specify the existing volume's name: website_pgdata.