from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("PySpark PostgreSQL") \
.config("spark.jars", "path\to\postgresql-42.3.1.jar") \
.getOrCreate()
# Read PostgreSQL DB table into dataframe
df = spark.read \
.format("jdbc") \
.option("url", "jdbc:postgresql://localhost:5432/analysis")\
.option("dbtable", "insurance") \
.option("user", "postgres") \
.option("password", "********") \
.option("driver", "org.postgresql.Driver") \
.load()
# Display database table schema
df.printSchema()