Question: So i am trying to wrap my head around what indexes in postgresql, I have understood so far that indexes help for faster querying and that all primary keys have an index, I was wondering if all foreign keys(referring to a primary key of another table) should also have an index or is it just the attributes in a table that we are querying that need indexes?

Thanks.

Answer: 
Indexes should be based primarily on queries that are being used. If you are going to be filtering or aggregating or sorting by the foreign key, then it is sensible as an index.

The other use is using indexes to enforce unique constraints.

In other words, there is no rule that foreign keys should always be indexed. It is often a good idea, but that depends on the queries you want to run.
It is not mandatory to index the foreign key. But if you ever delete from the referenced table, or update it in a way that changes the primary key, then it will have to search for rows in the referencing table by the foreign key column to make sure it will be left with no violations (or to set them to null, or delete them, depending on what action was defined in the foreign key). Without an index on the foreign key column this will likely be slow.

Also, it is likely you will want to run queries like "Show me all the detail rows of this master row", and without a index that will also be slow.

If you never delete from the referenced table and never run that kind of query, then you probably do not need the index.