An introduction to PostgreSQL
Foreign Data Wrappers
While some might claim that PostgreSQL is the "most admired and desired database" in the world [13], it is definitely not the only one. Sometimes you will need to grab data from another database. The good news is that you can do this without leaving PostgreSQL. PostgreSQL's Foreign Data Wrappers (FDWs) allow you to access data from external sources as if the data were residing in a local table. This is facilitated through the SQL Management of External Data (SQL/MED) extension. The server you connect to doesn't even need to run a different database engine – it could be just another PostgreSQL instance.
First, you need to ensure that the required FDW extension is installed. For example, if you're connecting to a MySQL database, you might need the mysql_fdw extension [14]. Next, define a foreign server, which represents the external data source. This includes the type of server (e.g., MySQL, another PostgreSQL server, etc.) and connection details. To connect to a different PostgreSQL instance, you would use the following:
CREATE SERVER my_foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'foreignhost', port '5432', dbname 'foreign_db');
Next, you need to create a user mapping to define which local user corresponds to which user on the foreign server; credentials can be included here:
CREATE USER MAPPING FOR local_user SERVER my_foreign_server OPTIONS (user 'foreign_user', password 'foreign_password');
At this point, you can define a foreign table that mirrors the structure of the table on the foreign server. This definition includes column names and types:
CREATE FOREIGN TABLE foreign_table ( id integer, data text ) SERVER my_foreign_server OPTIONS (schema_name 'public', table_name 'foreign_table');
That's it! A simple
SELECT * FROM foreign_table;
will work just like it would with a normal table on the local instance.
Be aware that querying foreign tables can be slower than local tables. Optimization strategies such as appropriate indexing on the foreign server, wise use of predicates in queries, and materialized views can help to mitigate this – at least to a certain extent. Moreover, FDW connections need to be maintained. Obviously schema changes on the foreign server require corresponding changes on the PostgreSQL side. Otherwise, your queries won't work as expected. For most scenarios, it is better to treat FDW as a quick, temporary workaround for a specific problem where a proper solution would be too expensive.
Conclusion
Due to space constraints, I described only a very limited set of features supported by PostgreSQL, leaving out such important areas as indexing (aside from default B-tree indexing, PostgreSQL supports many other options, such as GiST, GIN, or BRIN, that work better for particular scenarios), extensions, languages options for functions, stored procedures, and many others. However, I hope that even this little overview helps you to understand why PostgreSQL is so valued by users worldwide.
Whether you are a software or infrastructure engineer, you need to know at least one database well, and PostgreSQL is an excellent candidate. It can be as simple or as complex as desired: You can start small with little projects like the one described in this article and grow to complex setups in terms of the database itself as well as the supporting infrastructure. PostgreSQL is truly unique in its flexibility. You will not regret learning it.
This article was made possible by support from Percona LLC through Linux New Media's Topic Subsidy Program (https://www.linuxnewmedia.com/Topic_Subsidy).
Infos
- "Can PostgreSQL with its JSONB column type replace MongoDB?" by Yuriy Ivon, Medium, August 2, 2023, https://medium.com/@yurexus/can-postgresql-with-its-jsonb-column-type-replace-mongodb-30dc7feffaf3
- "Deliver Oracle Database 18c Express Edition in Containers" by Adrian Png, September 12, 2019, https://blogs.oracle.com/connect/post/deliver-oracle-database-18c-express-edition-in-containers
- "Postgres is eating the database world" by Vongg, Medium, March 14, 2024, https://medium.com/@fengruohang/postgres-is-eating-the-database-world-157c204dcfc4
- Pigsty: https://github.com/Vonng/pigsty/
- ParadeDB: https://www.paradedb.com/
- DuckDB: https://duckdb.org/
- Apache AGE: https://age.apache.org/
- FDWs: https://wiki.postgresql.org/wiki/Foreign_data_wrappers
- TimescaleDB: https://github.com/timescale/timescaledb
- ClickBench: https://benchmark.clickhouse.com/
- MVCC: https://en.wikipedia.org/wiki/Multiversion_concurrency_control
- "Data on Kubernetes, Deploying and Running PostgreSQL and Patterns for Databases in a Kubernetes Cluster" by Chris Milsted and Gabriele Bartolini, https://www.youtube.com/watch?v=99uSJXkKpeI
- "Postgres is the Undisputed Most Admired and Desired Database in Stack Overflow's 2023 Developer Survey" by Marc Linster, June 22, 2023, https://www.enterprisedb.com/blog/postgres-most-admired-database-in-stack-overflow-2023
- mysql_fdw for PostgreSQL: https://github.com/EnterpriseDB/mysql_fdw
« Previous 1 2 3 4 5
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.