DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • From Concept to Cloud: Building With Cursor and the Heroku MCP Server
  • Database Query Service With OpenAI and PostgreSQL in .NET
  • PostgreSQL 12 End of Life: What to Know and How to Prepare

Trending

  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Teradata Performance and Skew Prevention Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. Installing the Postgres LTREE Extension

Installing the Postgres LTREE Extension

Learn how to get the Postgres LTREE extension installed and enabled so that you can access special SQL operators and functions designed to support tree operations.

By 
Pat Shaughnessy user avatar
Pat Shaughnessy
·
Dec. 14, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

hidden inside of your postgres server is code that provides special sql operators and functions designed to support tree operations. it’s called the ltree extension . i’m guessing this stands for left-tree . in my next post, i’ll write about some of these functions and operators: what they do and how to use them.

but first, where is the ltree extension? how can you install and start using it? read on to find out.

testing whether the ltree extension is installed

depending on where you downloaded postgres from and how you installed it, you may have already installed ltree with postgres. to find out, execute this sql statement:

=> create extension ltree;
create extension

if you see the create extension message like this, then you’re all set! ltree was already installed and you just enabled it. skip to my next post when it's published to find out what it can do and how to use it.

but if you see:

=> create extension ltree;
error:  extension "ltree" already exists

…then your postgres server already had ltree enabled.

fyi : the pg_available_extensions table will show you all the postgres extensions that are available and installed on your server:

select * from pg_available_extensions;

  name   | default_version | installed_version |                     comment
---------+-----------------+-------------------+-------------------------------------------------
 ltree   | 1.1             | 1.1               | data type for hierarchical tree-like structures
 plpgsql | 1.0             | 1.0               | pl/pgsql procedural language
(2 rows)

as you can see, ltree already appears on my server’s list. the value 1.1 for installed_version indicates that i’ve already enabled it, too. this would have been blank before running the create extension ltree command above.

i originally installed a local copy of postgres on my mac using homebrew, and i was happy to find that the homebrew postgres formula does include steps to build and install ltree, after building the rest of the postgres server. but i still needed to enable it using create extension .

using ltree on a shared postgres server

running the create extension ltree command may fail with this error message:

=> create extension ltree;
error:  permission denied to create extension "ltree"
hint:  must be superuser to create this extension.

enabling postgres extensions requires super-user access. if you’re using a shared postgres server and don’t have super-user access, you’ll need to find someone who does. or you may just need to login to postgres using the proper postgres user account.

how to install the ltree extension

running the create extension command may also fail with this error message:

=> create extension ltree;
error:  could not open extension control file "/usr/local/pgsql/share/extension/ltree.control": no such file or directory

this error means the ltree code isn’t even installed on your postgres server. if you’re running on linux and installed postgres using a package manager, you may have to install a second package called postgresql-contrib .

if you installed postgres from source yourself, then you will see this error message because the postgres makefile doesn’t compile and install ltree by default. don’t worry! it turns out the postgres source tree already contains the code for ltree and many other extensions in a subdirectory called contrib .

compile it as follows:

$ cd /path/to/postgres-9.6.5/contrib/ltree
$ make

gcc -wall -wmissing-prototypes -wpointer-arith -wdeclaration-after-statement -wendif-labels -wmissing-format-attribute -wformat-security -fno-strict-aliasing -fwrapv -wno-unused-command-line-argument -o2  -dlower_node -i. -i. -i../../src/include   -c -o ltree_io.o ltree_io.c

etc…

$ sudo make install

/bin/sh ../../config/install-sh -c -d '/usr/local/pgsql/lib'
/bin/sh ../../config/install-sh -c -d '/usr/local/pgsql/share/extension'
/bin/sh ../../config/install-sh -c -d '/usr/local/pgsql/share/extension'
/usr/bin/install -c -m 755  ltree.so '/usr/local/pgsql/lib/ltree.so'
/usr/bin/install -c -m 644 ./ltree.control '/usr/local/pgsql/share/extension/'
/usr/bin/install -c -m 644 ./ltree--1.1.sql ./ltree--1.0--1.1.sql ./ltree--unpackaged--1.0.sql  ‘/usr/local/pgsql/share/extension/'

you can see above that the install step copied the ltree.so library into my postgres server’s lib directory, /usr/local/pgsql/lib , and ran a couple other commands, as well. now i can run the create extension ltree command as shown above. i don’t even need to restart postgres; it will find and load ltree.so automatically.

now that you have ltree installed and enabled, you can read my next post to learn how to use it.

PostgreSQL

Published at DZone with permission of Pat Shaughnessy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • From Concept to Cloud: Building With Cursor and the Heroku MCP Server
  • Database Query Service With OpenAI and PostgreSQL in .NET
  • PostgreSQL 12 End of Life: What to Know and How to Prepare

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: