jdbc_url = "jdbc:postgresql://localhost:5432/postgres" properties = {"user": "postgres","password": "postgrespwd","driver": "org.postgresql.Driver" }table_name = "player_seasons"df = spark.read.jdbc(url=jdbc_url, table=table_name, properties=properties)df.show()
When I ran above code, I got below error:
Py4JJavaError: An error occurred while calling o28.jdbc. : java.lang.ClassNotFoundException: org.postgresql.Driver
You need the PostgreSQL JDBC JAR.
Step 1: Download the JDBC driver
Go to the PostgreSQL site, or directly:
Step 2: Make the JAR available to Spark
There are three common ways:
Option 1: Copy to Spark jars/ directory
Option 2: Pass the JAR when creating SparkSession
Option 3: Use Maven coordinates (auto-download)
Step 3: Retry reading the table
⚡ Note
-
Make sure your PostgreSQL is accessible from WSL.
If PostgreSQL is running on Windows, use the WSL IP of Windows host, e.g.,jdbc:postgresql://<Windows-IP>:5432/postgres
localhostinside WSL points to WSL itself, not Windows.
jdbc_url = "jdbc:postgresql://172.26.64.1:5432/postgres" properties = {"user": "postgres","password": "postgrespwd","driver": "org.postgresql.Driver" }table_name = "player_seasons"df = spark.read.jdbc(url=jdbc_url, table=table_name, properties=properties)df.show()