How to fix Unterminated C string in PostgreSQL
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- Identify the unterminated string in your SQL query
- Add the missing closing quote
- If the string contains single quotes, escape them properly
- For stored functions, use dollar quoting
SELECT * FROM my_table WHERE name = 'valueUnterminated 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
- 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)
- Escaping issues: Not properly escaping single quotes that appear within string values (need to use '' or dollar quoting)
- 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"
Review your SQL statement carefully, paying close attention to single quotes. Look for patterns like:
SELECT * FROM my_table WHERE name = 'valueCompare 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.
Once you've located the unterminated string, add the closing single quote:
-- WRONG
SELECT * FROM users WHERE email = 'user@example.com;
-- CORRECT
SELECT * FROM users WHERE email = 'user@example.com';When your string value contains single quotes, you must escape them by doubling them or using dollar quoting:
-- 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$$;When defining functions or triggers in SQL, use dollar-quoted strings to avoid escaping issues entirely:
CREATE FUNCTION my_func() RETURNS text AS $$
BEGIN
RETURN 'This string can have single quotes without escaping';
END;
$$ LANGUAGE plpgsql;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 '%
Execute the corrected SQL in psql, PgAdmin, or your SQL client to confirm the error is resolved:
psql -U username -d database_name
# Paste your corrected query hereAdvanced 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
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Error Codes(Official Docs)