In this example, you:
- Create a PostgreSQL database and table, and insert data into the table
- Create a PostgreSQL user and assign all privileges on the table to the user
- Configure the PXF JDBC connector to access the PostgreSQL database
- Create a PXF readable external table that references the PostgreSQL table
- Read the data in the PostgreSQL table using PXF
- Create a PXF writable external table that references the PostgreSQL table
- Write data to the PostgreSQL table using PXF
- Read the data in the PostgreSQL table again
Create a PostgreSQL Table
Perform the following steps to create a PostgreSQL table named forpxf_table1
in the public
schema of a database named pgtestdb
, and grant a user named pxfuser1
all privileges on this table:
-
Identify the host name and port of your PostgreSQL server.
-
Connect to the default PostgreSQL database as the
postgres
user. For example, if your PostgreSQL server is running on the default port on the host namedpserver
:$ psql -U postgres -h pserver
-
Create a PostgreSQL database named
pgtestdb
and connect to this database:=# CREATE DATABASE pgtestdb; =# \connect pgtestdb;
-
Create a table named
forpxf_table1
and insert some data into this table:=# CREATE TABLE forpxf_table1(id int); =# INSERT INTO forpxf_table1 VALUES (1); =# INSERT INTO forpxf_table1 VALUES (2); =# INSERT INTO forpxf_table1 VALUES (3);
-
Create a PostgreSQL user named
pxfuser1
:=# CREATE USER pxfuser1 WITH PASSWORD 'changeme';
-
Assign user
pxfuser1
all privileges on tableforpxf_table1
, and exit thepsql
subsystem:=# GRANT ALL ON forpxf_table1 TO pxfuser1; =# \q
With these privileges,
pxfuser1
can read from and write to theforpxf_table1
table. -
Update the PostgreSQL configuration to allow user
pxfuser1
to accesspgtestdb
from each Greenplum Database host. This configuration is specific to your PostgreSQL environment. You will update the/var/lib/pgsql/pg_hba.conf
file and then restart the PostgreSQL server.
Configure the JDBC Connector
You must create a JDBC server configuration for PostgreSQL and synchronize the PXF configuration. The PostgreSQL JAR file is bundled with PXF, so there is no need to manually download it.
This procedure will typically be performed by the Greenplum Database administrator.
-
Log in to the Greenplum Database coordinator host:
$ ssh gpadmin@<coordinator>
-
Create a JDBC server configuration for PostgreSQL as described in Example Configuration Procedure, naming the server directory
pgsrvcfg
. Thejdbc-site.xml
file contents should look similar to the following (substitute your PostgreSQL host system forpgserverhost
):<?xml version="1.0" encoding="UTF-8"?>
jdbc.driver org.postgresql.Driver jdbc.url jdbc:postgresql://pgserverhost:5432/pgtestdb jdbc.user pxfuser1 jdbc.password changeme ```
-
Synchronize the PXF server configuration to the Greenplum Database cluster:
gpadmin@coordinator$ pxf cluster sync
Read from the PostgreSQL Table
Perform the following procedure to create a PXF external table that references the forpxf_table1
PostgreSQL table that you created in the previous section, and reads the data in the table:
-
Create the PXF external table specifying the
jdbc
profile. For example:gpadmin=# CREATE EXTERNAL TABLE pxf_tblfrompg(id int) LOCATION ('pxf://public.forpxf_table1?PROFILE=jdbc&SERVER=pgsrvcfg') FORMAT 'CUSTOM' (FORMATTER='pxfwritable_import');
-
Display all rows of the
pxf_tblfrompg
table:gpadmin=# SELECT * FROM pxf_tblfrompg; id ---- 1 2 3 (3 rows)
Write to the PostgreSQL Table
Perform the following procedure to insert some data into the forpxf_table1
Postgres table and then read from the table. You must create a new external table for the write operation.
-
Create a writable PXF external table specifying the
jdbc
profile. For example:gpadmin=# CREATE WRITABLE EXTERNAL TABLE pxf_writeto_postgres(id int) LOCATION ('pxf://public.forpxf_table1?PROFILE=jdbc&SERVER=pgsrvcfg') FORMAT 'CUSTOM' (FORMATTER='pxfwritable_export');
-
Insert some data into the
pxf_writeto_postgres
table. For example:=# INSERT INTO pxf_writeto_postgres VALUES (111); =# INSERT INTO pxf_writeto_postgres VALUES (222); =# INSERT INTO pxf_writeto_postgres VALUES (333);
-
Use the
pxf_tblfrompg
readable external table that you created in the previous section to view the new data in theforpxf_table1
PostgreSQL table:gpadmin=# SELECT * FROM pxf_tblfrompg ORDER BY id DESC; id ----- 333 222 111 3 2 1 (6 rows)
Content feedback and comments