How to fix Unterminated C string in PostgreSQL

PostgreSQLBEGINNERMEDIUMUpdated Dec 26, 2025ยท 183 views

This error occurs when a SQL string literal is missing its closing quote, causing PostgreSQL to treat the rest of the query as part of the string. Fix it by ensuring all quoted strings have matching opening and closing quotes.

Quick fix

Fixes Unterminated C string

SELECT * FROM my_table WHERE name = 'value
  1. Identify the unterminated string in your SQL query
  2. Add the missing closing quote
  3. If the string contains single quotes, escape them properly
  4. For stored functions, use dollar quoting
postgresql โ€” error
SELECT * FROM my_table WHERE name = 'value
Unterminated C string

What this error means

PostgreSQL's parser encountered a string that started with a quote but never found the matching closing quote. This causes the parser to treat all subsequent SQL code as part of the string rather than executable SQL. The error typically means your SQL syntax has an unclosed string literal that prevents the query from being properly parsed.

Typical symptoms

  • Query execution fails at parse time before reaching the database engine
  • Error message points to the end of the query or statement
  • Queries that contain single quotes fail unexpectedly
  • String values pasted from external sources cause parsing errors

Common causes

  1. Missing closing single quote ('): A string started with ' but never closed, like `SELECT * FROM users WHERE name = 'John`
  2. Null characters within strings: Embedded null characters (\0) cause C string routines to terminate prematurely
  3. Quote type confusion: Using double quotes for string values instead of single quotes (column names use double quotes, string values use single quotes)
  4. Escaping issues: Not properly escaping single quotes that appear within string values (need to use '' or dollar quoting)
  5. Line ending problems in scripts: Windows (CRLF) vs Unix (LF) line endings in SQL files can cause parsing issues

How to fix "Unterminated C string"

1Identify the unterminated string in your SQL query

Review your SQL statement carefully, paying close attention to single quotes. Look for patterns like:

sql
SELECT * FROM my_table WHERE name = 'value

Compare opening and closing quotes. Count single quotes in your query - they should appear in pairs. Use your editor's bracket matching feature to find mismatched quotes.

2Add the missing closing quote

Once you've located the unterminated string, add the closing single quote:

sql
-- WRONG
SELECT * FROM users WHERE email = 'user@example.com;

-- CORRECT
SELECT * FROM users WHERE email = 'user@example.com';
3If the string contains single quotes, escape them properly

When your string value contains single quotes, you must escape them by doubling them or using dollar quoting:

sql
-- Using doubled quotes
SELECT * FROM products WHERE description = 'John''s product';

-- Using dollar quoting (PostgreSQL-specific, cleaner)
SELECT * FROM products WHERE description = $$John's product$$;
4For stored functions, use dollar quoting

When defining functions or triggers in SQL, use dollar-quoted strings to avoid escaping issues entirely:

sql
CREATE FUNCTION my_func() RETURNS text AS $$
BEGIN
  RETURN 'This string can have single quotes without escaping';
END;
$$ LANGUAGE plpgsql;
5Check for null characters if the error persists

If you're inserting data from an application, ensure no null characters (\0) are present in string values. These can terminate C strings prematurely:

```sql
-- In PostgreSQL, check for null bytes
SELECT * FROM my_table WHERE my_column LIKE '%

6Verify the corrected query in your client

Execute the corrected SQL in psql, PgAdmin, or your SQL client to confirm the error is resolved:

bash
psql -U username -d database_name
# Paste your corrected query here

Advanced notes

PostgreSQL supports multiple string quoting mechanisms with different escaping rules. Single quotes (') require escaping other single quotes by doubling them ('') or by using dollar quoting ($$...$$, $tag$...$tag$). Dollar quoting is preferred in functions to avoid deep nesting of escape sequences. The "Unterminated C string" error specifically indicates that PostgreSQL's C parser reached the end of input while still looking for the closing quote. This is distinct from other SQL syntax errors because it's a lexical error that prevents parsing entirely. Line ending problems can manifest as unterminated string errors when SQL files created on Windows are executed on Unix systems, as the unexpected carriage returns can break string parsing. Always validate and sanitize user input before constructing SQL queries, though parameterized queries with prepared statements are the safest approach to avoid these issues entirely.

Frequently asked questions

What does "Unterminated C string" mean?
PostgreSQL's parser encountered a string that started with a quote but never found the matching closing quote. This causes the parser to treat all subsequent SQL code as part of the string rather than executable SQL. The error typically means your SQL syntax has an unclosed string literal that prevents the query from being properly parsed.
How do I fix "Unterminated C string"?
This error occurs when a SQL string literal is missing its closing quote, causing PostgreSQL to treat the rest of the query as part of the string. Fix it by ensuring all quoted strings have matching opening and closing quotes.
What causes "Unterminated C string"?
Missing closing single quote ('): A string started with ' but never closed, like SELECT * FROM users WHERE name = 'John Null characters within strings: Embedded null characters (\0) cause C string routines to terminate prematurely Quote type confusion: Using double quotes for string values instead of single quotes (column names use double quotes, string values use single quotes)
Is the "Unterminated C string" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading