0% found this document useful (0 votes)
106 views

Inserting PDF To Postgresql Database

This document discusses inserting PDF and CSV files into a PostgreSQL database. It first defines a table to store PDF files and metadata. It then creates a function to convert a PDF file to a bytea format for storage. Finally, it provides a sample query that inserts a specific PDF file into the table. For CSV import, it simply links to an external resource that explains how to use the Copy command to import CSV data into a PostgreSQL table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Inserting PDF To Postgresql Database

This document discusses inserting PDF and CSV files into a PostgreSQL database. It first defines a table to store PDF files and metadata. It then creates a function to convert a PDF file to a bytea format for storage. Finally, it provides a sample query that inserts a specific PDF file into the table. For CSV import, it simply links to an external resource that explains how to use the Copy command to import CSV data into a PostgreSQL table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Inserting PDF to PostgreSQL database


Defining the table

CREATE TABLE public.table1


(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1
MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
"documentFile" bytea,
name text COLLATE pg_catalog."default",
CONSTRAINT table1_pkey PRIMARY KEY (id)
)

First, we need to define the function that will convert the PDF document to a bytea format

Function name is bytea_import

create or replace function bytea_import(p_path text, p_result out bytea)


language plpgsql
as $$
declare
l_oid oid;
begin
select lo_import(p_path) into l_oid;
select lo_get(l_oid) INTO p_result;
perform lo_unlink(l_oid);
END;
$$;

Sample query to insert a PDF file that is from 'D:/OneDrive/Documents/Sample PDF file.pdf’

INSERT INTO table1("documentFile", "name") VALUES(bytea_import('D:/OneDrive/Documents/Sample PDF


file.pdf'), 'sampledoc');

2. Import CSV to a table

I found this link that clearly explains the command Copy and how it was use to import CSV file to a table.
I am not sure what to add here.

https://dataschool.com/learn-sql/importing-data-from-csv-in-postgresql/

You might also like