Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:13:59+00:00 2026-05-31T09:13:59+00:00

I have two files that both work fine when executing them in Postgres. File

  • 0

I have two files that both work fine when executing them in Postgres.

File 1

CREATE TABLE ContractStatusEnum (
    id              SERIAL PRIMARY KEY,
    description         VARCHAR(4000) NOT NULL
);

*File 2

DO $$ BEGIN

    IF NOT EXISTS (SELECT * FROM ContractStatusEnum WHERE id = 1) THEN
    INSERT INTO ContractStatusEnum (id, description) VALUES
        (1, 'Ordered'),
        (2, 'Active'),
        (3, 'Stopped'),
        (4, 'Canceled'),
        (10, 'Inactive');
    END IF;
END $$

However, if I just put them together in one file, it fails with

ERROR: syntax error at or near “DO”
LINE 6: DO $$ BEGIN

On MS SQL I can solve this by creating a new context by putting a GO statement between two scripts. Any clue how to do it in Postgres 9?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:14:00+00:00Added an answer on May 31, 2026 at 9:14 am

    You need to terminate your DO block with a semicolon.

    The following works for me:

    DO $$ BEGIN
    
        IF NOT EXISTS (SELECT * FROM ContractStatusEnum WHERE id = 1) THEN
        INSERT INTO ContractStatusEnum (id, description) VALUES
            (1, 'Ordered'),
            (2, 'Active'),
            (3, 'Stopped'),
            (4, 'Canceled'),
            (10, 'Inactive');
        END IF;
    END $$
    ;
    

    I saved the statements in f1.sql (create table) and f2.sql (do block):

    psql (9.1.3)
    Type "help" for help.
    
    postgres=> \i f1.sql
    CREATE TABLE
    postgres=> \i f2.sql
    DO
    postgres=> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two string variables which are both file paths. The code that worked
I have two functions that loads on document ready. They work fine when they
I have two PHP files that I need to link. How can I link
I have two properties files that are not the same and I need to
I have two XML files that are generated by another application I have no
I have two text files that contain columnar data of the variety position -
Suppose that you have two huge files (several GB) that you want to concatenate
I have two large (~100 GB) text files that must be iterated through simultaneously.
I have this c# web app that relies heavily on two dll files that
Is there any way to list all the files that have changed between two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.