• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

PostgreSQL Command: CREATE FOREIGN TABLE

January 2, 2024

PostgreSQL Command: CREATE FOREIGN TABLE

PostgreSQL is a powerful open-source relational database management system that offers a wide range of features and capabilities. One of its notable features is the ability to create foreign tables, which allow you to access data from external sources as if they were regular database tables. In this article, we will explore the PostgreSQL command CREATE FOREIGN TABLE and how it can be used to integrate external data into your database.

What is a Foreign Table?

A foreign table in PostgreSQL is a table that represents data from an external source, such as another database, a CSV file, or a web service. It provides a way to access and manipulate this external data using SQL commands, just like you would with regular database tables. The data in a foreign table is not stored in the PostgreSQL database itself but is accessed in real-time from the external source.

Creating a Foreign Table

The CREATE FOREIGN TABLE command is used to define a foreign table in PostgreSQL. Here is the basic syntax:

CREATE FOREIGN TABLE table_name (
    column_name1 data_type1,
    column_name2 data_type2,
    ...
)
SERVER server_name
OPTIONS (option1 'value1', option2 'value2', ...);

Let’s break down the syntax:

  • table_name: The name of the foreign table you want to create.
  • column_name: The name of each column in the foreign table, along with its data type.
  • server_name: The name of the foreign server that provides access to the external data source.
  • OPTIONS: Additional options that can be specified for the foreign table, such as the file format, delimiter, or connection parameters.

Example: Creating a Foreign Table from a CSV File

Let’s say you have a CSV file called employees.csv that contains information about employees, including their names, ages, and salaries. You can create a foreign table in PostgreSQL to access this data using the following command:

CREATE FOREIGN TABLE employees (
    name text,
    age integer,
    salary numeric
)
SERVER csv_server
OPTIONS (filename '/path/to/employees.csv', format 'csv', header 'true');

In this example:

  • employees is the name of the foreign table.
  • name, age, and salary are the columns in the foreign table, along with their respective data types.
  • csv_server is the name of the foreign server that provides access to the CSV file.
  • filename specifies the path to the CSV file.
  • format indicates that the file is in CSV format.
  • header specifies that the first row of the CSV file contains column headers.

Once the foreign table is created, you can query it just like any other table in your PostgreSQL database. For example, you can retrieve all employees with a salary greater than $50,000 using the following SQL statement:

SELECT * FROM employees WHERE salary > 50000;

Summary

The CREATE FOREIGN TABLE command in PostgreSQL allows you to create tables that represent data from external sources. It provides a convenient way to integrate external data into your database and access it using SQL commands. Whether you need to access data from another database, import data from a file, or connect to a web service, foreign tables offer a flexible and powerful solution.

If you are interested in learning more about PostgreSQL and its features, consider exploring Server.HK, a leading VPS hosting company that offers reliable and high-performance hosting solutions. With their expertise in PostgreSQL and other technologies, they can help you optimize your database performance and ensure the smooth operation of your applications.

Recent Posts

  • How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  • How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  • CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  • Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  • Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot