| Hosted by CoCalc | Download

CoCalc command line test of postgresql with jdbc

  1. follow instructions for 2 minute install of postgresql, including creating a tiny database test

  2. if you plan to use LibreOffice Base to browse the database, add a primary key to the table before running Base

    ALTER TABLE people ADD COLUMN id SERIAL PRIMARY KEY;
  3. stop the database server

    pg_ctl -D /home/user/postgres_data stop
  4. modify /home/user/postgres_data/postgresql.conf to use a TCP port by changing the end of the file:

    #unix_socket_directories = '/home/user/postgres_data' unix_socket_permissions = 0700 unix_socket_directories = '/tmp' port = 15432
  5. start the database server and look at test db

    pg_ctl -D /home/user/postgres_data -l /home/user/postgres_data/logfile start psql test -h localhost -p 15432
  6. download PostgreSQL JDBC 4.2 Driver, 42.2.5 to ~/pgjar

  7. edit JDBCExample.java; code is based on examples at Connect to PostgreSQL with JDBC driver and JDBC - Select Records Example

  8. compile and run the test program

    cd ~/pgjar javac JDBCExample.java export CLASSPATH=/home/user/pgjar/postgresql-42.2.5.jar:/home/user/pgjar java JDBCExample ==> ~/pgjar$ java JDBCExample Picked up _JAVA_OPTIONS: -Djava.io.tmpdir=/home/user/tmp/ -Xms64m -------- PostgreSQL JDBC Connection Testing ------------ PostgreSQL JDBC Driver Registered. Age: 10, Name: joe Age: 20, Name: fred Connection test succeeded.
  9. additional references