PostgreSQL insert into select
In this tutorial, we will learn how to insert data from a database hosted on a remote server into a database in our local computer. We will use the remote service provider Heroku PostgreSQL, which offers a free plan for testing purposes.
Insert data from a database hosted on a remote server into a database in your local machine
Create a new application and add a PostgreSQL datastore. You can 设置
access the credentials to connect to the remote server by clicking the link.
To connect to the remote database, use the following command and make sure you are not connected to the corporate network to avoid timeout errors due to communication failures.
>psql -h ec2-52-49-56-163.eu-west-1.compute.amazonaws.com -d dagbp3p323638g -U cvkmaeedgzlxdr
Output:
dagbp3p323638g=>
Create a remote_employee
table named that will hold records containing employee information such as id
, first name
, last name
and email
.
dagbp3p323638g=> create table remote_employee(id SERIAL NOT NULL UNIQUE,first_name varchar(50),last_name varchar(50),email varchar(50),PRIMARY KEY(id));
CREATE TABLE
Add three records to remote_employee
the table to ensure that we have data to fetch from the remote server to the local server.
dagbp3p323638g=> insert into remote_employee(first_name,last_name,email)values ('john','doe','john@gmail.com');
INSERT 0 1
dagbp3p323638g=> insert into remote_employee(first_name,last_name,email)values ('peter','parker','peter@gmail.com');
INSERT 0 1
dagbp3p323638g=> insert into remote_employee(first_name,last_name,email)values ('mary','public','mary@gmail.com');
INSERT 0 1
The following query verifies that our three records have been inserted into the database.
dagbp3p323638g=> select * from remote_employee;
id | first_name | last_name | email
----+------------+-----------+-----------------
1 | john | doe | john@gmail.com
2 | peter | parker | peter@gmail.com
3 | mary | public | mary@gmail.com
(3 rows)
You must connect to the local server and create a local_database
database called that will contain the local entities to be populated with data from the remote entities.
>psql -U postgres
postgres=# create database local_database;
CREATE DATABASE
postgres=# \c local_database;
You are now connected to database "local_database" as user "postgres".
Create a local_employee
table named that contains remote_employee
the same columns as , because they contain the same data types.
local_database=# create table local_employee(id SERIAL NOT NULL UNIQUE,first_name varchar(50),last_name varchar(50),email varchar(50),PRIMARY KEY(id));
CREATE TABLE
dblink
Insert
in PostgreSQL usingSelect
dblink
is an extension which allows us to query data from other databases. We create an extension using the following command.
local_database=# create extension dblink;
Before getting data from the remote server, we can dblink_connect
test the connection using as shown below. If the query returns OK, this indicates that our remote database has been connected successfully and we can now execute insert, update, and delete queries.
Note the connection name temp_conn
, we will use it in our insert query.
local_database=# SELECT dblink_connect('temp_conn', 'dbname=dagbp3p323638g port=5432 host=ec2-52-49-56-163.eu-west-1.compute.amazonaws.com user=cvkmaeedgzlxdr password=336ea6e67129e8f082140f1b60954dafa33940f17b02e1f580ea45f10401f85e');
dblink_connect
----------------
OK
(1 row)
The query that inserts data into our local_employee
table consists of an insert statement and a select statement to our remote server.
Use to execute a select statement to obtain data
by specifying the connection name temp_conn
and SQL string .dblink
local_database=# INSERT INTO local_employee
SELECT id,first_name,last_name,email FROM dblink('temp_conn','SELECT id, first_name, last_name, email FROM remote_employee') AS temp_employee(id integer,first_name varchar(50),last_name varchar(50),email varchar(50));
INSERT 0 3
remote_employee
All records in temp_employee
are returned as a temporary table and dblink
are successfully inserted into the local table by utilizing the connection local_employee
.
local_database=# select * from local_employee;
Output:
id | first_name | last_name | email
----+------------+-----------+-----------------
1 | john | doe | john@gmail.com
2 | peter | parker | peter@gmail.com
3 | mary | public | mary@gmail.com
(3 rows)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab