EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL EXCLUDE
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL EXCLUDE

By Priya PedamkarPriya Pedamkar

PostgreSQL EXCLUDE

Introduction to PostgreSQL EXCLUDE

PostgreSQL excludes statements in PostgreSQL is used to compare any two rows from the specified column or expression by using the operator specified in PostgreSQL. At the time of excluding the column, the comparison operator will return the null or false value as output. We can also exclude table data and schema from the backup which we have taken from using pg_dump command in PostgreSQL. We exclude the specified constraint in PostgreSQL, we have also excluding the gist index from the table.

Syntax

Below is the syntax of exclud in PostgreSQL.

1. Exclude the constraint at the time of table creation:

create table name_of_table (name_of_column1 data_type, name_of_column2 data_type,name_of_column3 data_type, …., name_of_columnN data_type) EXCLUDE USING constraint_name (name_of_column with operator, name_of_column with operator));

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

2. Exclude table from backup:

pg_dump –U name_of_user –W password –d name_of_database –exclude-table name_of_table > backup_file_name.sql

3. Exclude schema from backup:

pg_dump –U name_of_user –W password –d name_of_database –exclude-schema name_of_schema> backup_file_name.sql

4. Exclude table data from backup:

pg_dump –U name_of_user –W password –d name_of_database –exclude-table-data= name_of_table > backup_file_name.sql

Parameters

Below is the parameter description syntax of exclud in PostgreSQL:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,700 ratings)
  • Create table: This parameter is used in exclude statements of gist index at the time of index creation.
  • Name of the table: Table name is defined as creating a new table and defined the name to the same table at that table exclude the gist index from the same.
  • Name of the column: Column name is defined as to create a table with a specified column by creating exclude constraint on the same.
  • Data type: Define the data type of column and use exclude constraint on the same.
  • Exclude: Exclude is used to exclude the constraint from the table in PostgreSQL. We have excluding constraint from the table column in PostgreSQL.
  • Constraint name: This is defined as we have excluding specified constraints at the time of table creation in PostgreSQL.
  • Operator: We have used the operator while excluding constraints from the table.
  • Pg_dump: This command is used to take a dump of the database by excluding the specified table in PostgreSQL.
  • Exclude table: This parameter is used to exclude a specified table from the whole database dump.
  • Exclude schema: This parameter is used to exclude specified schema from the whole database dump.
  • Exclude table data: This parameter is used to exclude table data from the whole database dump.

How EXCLUDE statement work in PostgreSQL

Below is the working of Exclude statements:

We have excluded the constraint in PostgreSQL by using the exclude keyword in PostgreSQL. We have also used except operator to exclude the value from the other table in PostgreSQL. Using except we need to define the specific column name in the second table which was we have defined in the first table. To execute the exclude statement in PostgreSQL we need to have created table privileges on a table, create backup privileges or we need to have superuser privileges to execute statements.

Below is the example of Exclude statement requires to create table privileges on a table, create backup privileges or superuser privileges to execute the exclude statement in PostgreSQL.

Code:

pg_dump -U db_test -W -d testing --exclude-table stud1 > stud.sql
pg_dump -U postgres -W -d testing --exclude-table stud1 > stud.sql
echo $?

Output:

PostgreSQL EXCLUDE1

Explanation: In the above first example, we have used user as db_test, this user doesn’t have privileges of creating a table or superuser so, it will issue error while executing the exclude statement with pg_dump. In the second example, we have taken database backup using the username as Postgres, after using this user we have taken the database backup.

Examples to Implement PostgreSQL EXCLUDE

Below are the examples of the exclude statements:

Example #1: Exclude the constraint at the time of index creation

The below example shows that exclude the index constraint at the time of table creation. We have created a table name as test_exclude to exclude the constraint.

Code:

CREATE TABLE test_exclude(ID INT PRIMARY KEY NOT NULL, STUD_NAME TEXT, STUD_AGE INT, STUD_ADDRESS CHAR(50), EXCLUDE USING gist (STUD_NAME WITH =, STUD_AGE WITH <>));
\d+ test_exclude;

Output:

PostgreSQL EXCLUDE2

Example #2: Exclude table data by using except operator

Below is the example of Exclude the table data by using except operator in PostgreSQL.

We have used stud1 and stud2 table to exclude the data from the table.

PostgreSQL EXCLUDE3

Example #3: Exclude the table from a whole database backup

The below example shows that excludes the table from the whole database backup. We have excluded the stud1 table from a testing database backup.

Code:

pg_dump -U postgres -W -d testing --exclude-table stud1 > stud.sql
echo $?

Output:

table from a whole database backup

Example #4: Exclude the schema from a whole database backup

The below example show that excludes the schema from the whole database backup. We have excluded test schema from a testing database backup.

Code:

pg_dump -U postgres -W -d testing --exclude-schema test> stud.sql
echo $?

Output:

schema from a whole database backup

Advantages of PostgreSQL EXCLUDE

Below are the advantages :

  • Using pg_dump we can exclude the specific table as well as schema from the whole database dump in PostgreSQL.
  • Using exclude statements we can also exclude the constraint in PostgreSQL.
  • Using pg_dump we can exclude the table data from the whole database dump in PostgreSQL.
  • We have excluded the two table data by using except statements in PostgreSQL.

Recommended Articles

This is a guide to PostgreSQL EXCLUDE. Here we discuss an introduction to PostgreSQL EXCLUDE, syntax, how does it work, advantages and query examples. You can also go through our other related articles to learn more –

  1. Install PostgreSQL
  2. Array in PostgreSQL
  3. hstore in PostgreSQL
  4. PostgreSQL REINDEX
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more