Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading data from a CSV file using <loadUpdateData> Postgres

Tags:

liquibase

I would appreciate some suggestions. I am accomplishing a simple load of one table with five records from a CSV file. When I load the table, I get the below the error:

liquibase.exception.DatabaseException: org.postgresql.util.PSQLException: ERROR: zero-length delimited identifier at or near """"

This is by csv file

1,Nate Happy,[email protected],1761 Brookview Trail,(205) 555-1212
2,Brigette Happy,[email protected],7507 Meadowgate Lane,(704) 555-1212
3,Katie Happy,[email protected],7507 Meadowgate Lane,(704) 555-1212
4,Lauren Happy,[email protected],7507 Meadowgate Lane,(704) 555-1212
5,Jackson Hope,[email protected],7507 Meadowgate Lane,(704) 555-1212

This is my changeset for loading the data

<changeSet id="6-loadData" author="liquibase" dbms="postgresql" >
<preConditions onErrorMessage="Failed Pre Conditions for table" onFail="HALT"> 
      <and>
      <tableExists schemaName="public" tableName="contact" />
      <sqlCheck expectedResult ="1">SELECT COUNT(*) contact</sqlCheck>
      </and>
</preConditions>
<comment>Adding Data...</comment>
   <loadUpdateData catalogName="pg_catalog"
        encoding="UTF-8"
        file="src/main/resources/data/contacts.csv"
        primaryKey="contact_id"
        quotchar="A String"
        schemaName="public"
        separator=","
        tableName="contact">
      <column name="contact_id" type="int" />
      <column name="contact_name" type="varchar(45)"/>
      <column name="email" type="varchar(45)" />
      <column name="address" type="varchar(45)" />
      <column name="telephone" type="varchar(45)" />
   </loadUpdateData>

This is my changeset for creating the table:

   <changeSet id="4 Create Table" author="liquibase" runAlways="true">
   <preConditions onErrorMessage="Failed Pre Conditions for table" onFail="MARK_RAN"> 
       <not><tableExists schemaName="public" tableName="contact"/> </not>
   </preConditions>
   <comment>Creating Table named: Contact...</comment>
   <createTable tableName="contact" schemaName="public">
      <column name="contact_id" type="int" >
      <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="contact_name" type="varchar(45)">
         <constraints nullable="false"/>
      </column>
      <column name="email" type="varchar(45)">
         <constraints nullable="false"/>
      </column>
      <column name="address" type="varchar(45)">
         <constraints nullable="false"/>
      </column>
      <column name="telephone" type="varchar(45)">
         <constraints nullable="false"/>
      </column>
   </createTable>

Here is the sequence I am using to the primary key (contact_id)

   <changeSet id="2-Create Sequence" author="liquibase" runAlways="true">
   <preConditions onErrorMessage="Failed Pre Conditions for sequence" onFail="MARK_RAN"> 
      <not><sequenceExists schemaName="public" sequenceName="contactid_seq" /></not>
    </preConditions>
   <comment>Creating Sequence...</comment>
   <createSequence sequenceName="contactid_seq"
                    incrementBy="1"
                    minValue="1"
                    maxValue="9223372036854775807"
                    startValue="1"
                    ordered="1"
                    schemaName="public"/>

This is how I am using the constraint:

   <changeSet id="5-Add Constraint" author="liquibase">
     <comment>Adding contactid_seq sequence to Contact table...</comment> 
     <addDefaultValue catalogName="pg_catalog"
        columnDataType="int"
        columnName="contact_id"
        tableName="contact"
        schemaName="public"
        defaultValueSequenceNext="contactid_seq" />

Thanks for taking the time to read my post.

Russ

like image 831
rray Avatar asked Dec 06 '14 16:12

rray


People also ask

How do I import a CSV file into PostgreSQL using Python?

First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2. connect() method. before importing a CSV file we need to create a table. In the example below, we created a table by executing the “create table” SQL command using the cursor.

How do I import a CSV file into PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.


2 Answers

I discovered the CSV file was missing the HEADER column names. The LoadUpdateData's column elements use specific Java types, such as String instead of VARCHAR(45) and NUMERIC instead of "int". Once I corrected these two errors, I was successful.

like image 187
rray Avatar answered Dec 31 '22 18:12

rray


Don't forget to change a string of quotchar, if you set it like in manual,

quotchar="A String"

then Liquibase will mark letter "A" as quotes.

You can exclude it from changeset and use default html quotes charset

like image 41
Andriy Riabby Avatar answered Dec 31 '22 19:12

Andriy Riabby