1

I have created a PostgreSQL database schema and I want to connect an android application with the database. I get this error:

"W/System.err: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."

Here is my code:

public class Database {

    private Connection connection;

    private final String host = "localhost";
    private final String database = "postgres";
    private final int port = 5432;
    private final String user = "postgres";
    private final String pass = "password";
    private String url = "jdbc:postgresql://%s:%d/%s";
    private boolean status;

    public Database() {
        this.url = String.format(this.url, this.host, this.port, this.database);
        connect();
        //this.disconnect();
        System.out.println("connection status:" + status);
    }

    private void connect() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("org.postgresql.Driver");
                    connection = DriverManager.getConnection(url, user, pass);
                    status = true;
                    System.out.println("connected:" + status);
                } catch (Exception e) {
                    status = false;
                    System.out.print(e.getMessage());
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            this.status = false;
        }
    }

    public Connection getExtraConnection(){
        Connection c = null;
        try {
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection(url, user, pass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return c;
    }
}
public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database db = new Database();
    }
}

When I run netstat -a, it's all ok, it accepts TCP connections. I can not find anywhere anything that helps for connecting an Android application with a local PostgreSQL database. What is the problem and how can I fix this?

Programmer2B
  • 552
  • 3
  • 14

3 Answers3

0

You are missing Class.forName("org.postgresql.Driver"), above DriverManager.getConnection

Profeta
  • 13
  • 2
0

localhost:5432 is accessible from the device? if you test in a real device your PostgreSQL server must be in your device network or if the device is connected with the USB cable you can use the ADB port forward to connect to the server.

Golil
  • 467
  • 4
  • 12
0

Try changing replacing the host line with this:

private final String host = "10.0.2.2";
Ali
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '21 at 23:14
  • Now I get another error: W/System.err: org.postgresql.util.PSQLException: SCRAM authentication is not supported by this driver. You need JDK >= 8 and pgjdbc >= 42.2.0 (not ".jre" versions) When I change implementation 'org.postgresql:postgresql:42.2.5.jre7' to implementation 'org.postgresql:postgresql:42.3.1', then I get another error... – Programmer2B Dec 28 '21 at 23:41