# PostgreSQL SQL Parser
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[](https://pypi.org/project/python-pgsql-parser/)
A high-performance PostgreSQL SQL parser for extracting comprehensive database schema metadata from DDL scripts.
## Features
- **Complete SQL Parsing**: Tokenizes and parses PostgreSQL DDL statements
- **Schema Metadata Extraction**:
- Database, schema, and table names
- Table types (regular, temporary, view, materialized view)
- Column definitions with data types, lengths, precision, and constraints
- Primary keys, foreign keys, indexes, and constraints
- **Advanced SQL Support**:
- Quoted identifiers
- ANSI SQL and PostgreSQL-specific syntax
- CREATE/ALTER TABLE statements
- View and materialized view definitions
- **Powerful API**:
- Parse entire scripts or individual statements
- Retrieve tables by qualified name
- Iterate through parsed statements
- **Well-Tested**: Comprehensive test suite with 95%+ coverage
## Installation
```bash
pip install python-pgsql-parser
```
## Quick Start
```python
from pgsql_parser import SQLParser
# Initialize parser
parser = SQLParser()
# Parse SQL script
sql_script = """
CREATE TABLE public.users (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(255) UNIQUE
);
CREATE VIEW user_emails AS
SELECT id, email FROM public.users;
"""
parser.parse_script(sql_script)
# Get all tables
tables = parser.get_tables()
for table in tables:
print(f"Table: {table.schema}.{table.name} ({table.table_type})")
# Get specific table
users_table = parser.get_table("users", schema="public")
if users_table:
print(f"\nColumns in users table:")
for col_name, column in users_table.columns.items():
print(f"- {col_name}: {column.data_type}")
```
## Documentation
### Core Classes
- `SQLParser`: Main parser class
- `Table`: Represents a table/view definition
- `Column`: Contains column metadata
- `PrimaryKey`, `ForeignKey`, `Constraint`: Schema constraint objects
### Key Methods
- `parse_script(sql_script)`: Parse entire SQL script
- `parse_statement(sql)`: Parse single SQL statement
- `get_tables()`: Get all parsed tables/views
- `get_table(name, schema, database)`: Get specific table by qualified name
- `statement_generator(sql_script)`: Iterate through SQL statements
## Usage Examples
### Extract Table Metadata
```python
table = parser.get_table("users", "public")
print(f"Table: {table.schema}.{table.name}")
print(f"Type: {table.table_type}")
print(f"Columns: {len(table.columns)}")
print(f"Primary Key: {table.primary_key.columns if table.primary_key else None}")
print("\nColumn Details:")
for name, column in table.columns.items():
print(f"- {name}: {column.data_type}, " +
f"Nullable: {column.nullable}, " +
f"Primary Key: {column.is_primary}")
```
### Handle ALTER TABLE Statements
```python
sql = """
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP,
ADD CONSTRAINT fk_country
FOREIGN KEY (country_id) REFERENCES countries(id);
"""
parser.parse_statement(sql)
table = parser.get_table("users")
print(f"New column: {table.columns['last_login'].data_type}")
print(f"New foreign key: {table.foreign_keys[-1].ref_table}")
```
### Process Large Scripts
```python
with open("schema.sql", "r") as f:
sql_script = f.read()
for statement in parser.statement_generator(sql_script):
parser.parse_statement(statement)
# Process tables incrementally
new_tables = parser.get_tables()
for table in new_tables:
save_to_database(table)
```
## Contributing
Contributions are welcome! Please follow these steps:
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/your-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin feature/your-feature`)
5. Open a pull request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Support
For issues and feature requests, please [open an issue](https://github.com/yourusername/python-pgsql-parser/issues).
Raw data
{
"_id": null,
"home_page": "https://github.com/yourusername/python-pgsql-parser",
"name": "python-pgsql-parser",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "sql, parser, postgresql, ddl, metadata, schema, analysis",
"author": "Sunny Liu",
"author_email": "Sunny Liu <sunnyliu2@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/56/24/d7ebb26efe32584ca5c438c9c14aec1453fe300536edd694d9b04960cfa5/python_pgsql_parser-0.1.2.tar.gz",
"platform": null,
"description": "# PostgreSQL SQL Parser\n\n[](https://www.python.org/downloads/)\n[](https://opensource.org/licenses/MIT)\n[](https://pypi.org/project/python-pgsql-parser/)\n\nA high-performance PostgreSQL SQL parser for extracting comprehensive database schema metadata from DDL scripts.\n\n## Features\n\n- **Complete SQL Parsing**: Tokenizes and parses PostgreSQL DDL statements\n- **Schema Metadata Extraction**:\n - Database, schema, and table names\n - Table types (regular, temporary, view, materialized view)\n - Column definitions with data types, lengths, precision, and constraints\n - Primary keys, foreign keys, indexes, and constraints\n- **Advanced SQL Support**:\n - Quoted identifiers\n - ANSI SQL and PostgreSQL-specific syntax\n - CREATE/ALTER TABLE statements\n - View and materialized view definitions\n- **Powerful API**:\n - Parse entire scripts or individual statements\n - Retrieve tables by qualified name\n - Iterate through parsed statements\n- **Well-Tested**: Comprehensive test suite with 95%+ coverage\n\n## Installation\n\n```bash\npip install python-pgsql-parser\n```\n\n## Quick Start\n\n```python\nfrom pgsql_parser import SQLParser\n\n# Initialize parser\nparser = SQLParser()\n\n# Parse SQL script\nsql_script = \"\"\"\nCREATE TABLE public.users (\n id SERIAL PRIMARY KEY,\n name VARCHAR(50) NOT NULL,\n email VARCHAR(255) UNIQUE\n);\n\nCREATE VIEW user_emails AS\nSELECT id, email FROM public.users;\n\"\"\"\n\nparser.parse_script(sql_script)\n\n# Get all tables\ntables = parser.get_tables()\nfor table in tables:\n print(f\"Table: {table.schema}.{table.name} ({table.table_type})\")\n\n# Get specific table\nusers_table = parser.get_table(\"users\", schema=\"public\")\nif users_table:\n print(f\"\\nColumns in users table:\")\n for col_name, column in users_table.columns.items():\n print(f\"- {col_name}: {column.data_type}\")\n```\n\n## Documentation\n\n### Core Classes\n\n- `SQLParser`: Main parser class\n- `Table`: Represents a table/view definition\n- `Column`: Contains column metadata\n- `PrimaryKey`, `ForeignKey`, `Constraint`: Schema constraint objects\n\n### Key Methods\n\n- `parse_script(sql_script)`: Parse entire SQL script\n- `parse_statement(sql)`: Parse single SQL statement\n- `get_tables()`: Get all parsed tables/views\n- `get_table(name, schema, database)`: Get specific table by qualified name\n- `statement_generator(sql_script)`: Iterate through SQL statements\n\n## Usage Examples\n\n### Extract Table Metadata\n\n```python\ntable = parser.get_table(\"users\", \"public\")\n\nprint(f\"Table: {table.schema}.{table.name}\")\nprint(f\"Type: {table.table_type}\")\nprint(f\"Columns: {len(table.columns)}\")\nprint(f\"Primary Key: {table.primary_key.columns if table.primary_key else None}\")\n\nprint(\"\\nColumn Details:\")\nfor name, column in table.columns.items():\n print(f\"- {name}: {column.data_type}, \" +\n f\"Nullable: {column.nullable}, \" +\n f\"Primary Key: {column.is_primary}\")\n```\n\n### Handle ALTER TABLE Statements\n\n```python\nsql = \"\"\"\nALTER TABLE users \nADD COLUMN last_login TIMESTAMP,\nADD CONSTRAINT fk_country \n FOREIGN KEY (country_id) REFERENCES countries(id);\n\"\"\"\n\nparser.parse_statement(sql)\ntable = parser.get_table(\"users\")\nprint(f\"New column: {table.columns['last_login'].data_type}\")\nprint(f\"New foreign key: {table.foreign_keys[-1].ref_table}\")\n```\n\n### Process Large Scripts\n\n```python\nwith open(\"schema.sql\", \"r\") as f:\n sql_script = f.read()\n\nfor statement in parser.statement_generator(sql_script):\n parser.parse_statement(statement)\n \n # Process tables incrementally\n new_tables = parser.get_tables()\n for table in new_tables:\n save_to_database(table)\n```\n\n## Contributing\n\nContributions are welcome! Please follow these steps:\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/your-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin feature/your-feature`)\n5. Open a pull request\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Support\n\nFor issues and feature requests, please [open an issue](https://github.com/yourusername/python-pgsql-parser/issues).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Advanced PostgreSQL SQL parser for extracting database schema metadata",
"version": "0.1.2",
"project_urls": {
"Documentation": "https://github.com/devsunny/python-pgsql-parser/blob/main/docs/index.md",
"Homepage": "https://github.com/devsunny/python-pgsql-parser",
"Source": "https://github.com/devsunny/python-pgsql-parser",
"Tracker": "https://github.com/devsunny/python-pgsql-parser/issues"
},
"split_keywords": [
"sql",
" parser",
" postgresql",
" ddl",
" metadata",
" schema",
" analysis"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "63514b78df35c8d63425e75b9c889a913089d6fce680ec05038ffff9febeeaa6",
"md5": "07ed9fc5a190e0c723fa612237114459",
"sha256": "0f671ec6392506f0f4db15e027b69b19436ee1a2983c50cfb88f942e3e1ac4cf"
},
"downloads": -1,
"filename": "python_pgsql_parser-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "07ed9fc5a190e0c723fa612237114459",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 12132,
"upload_time": "2025-07-12T21:47:47",
"upload_time_iso_8601": "2025-07-12T21:47:47.250001Z",
"url": "https://files.pythonhosted.org/packages/63/51/4b78df35c8d63425e75b9c889a913089d6fce680ec05038ffff9febeeaa6/python_pgsql_parser-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5624d7ebb26efe32584ca5c438c9c14aec1453fe300536edd694d9b04960cfa5",
"md5": "32f0c59a1e365c0feb4c24c927d7f816",
"sha256": "acf045630c01b44d8ca291eaa9f7b525258b37cbec18a755166fc45e8655c8b8"
},
"downloads": -1,
"filename": "python_pgsql_parser-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "32f0c59a1e365c0feb4c24c927d7f816",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 17363,
"upload_time": "2025-07-12T21:47:47",
"upload_time_iso_8601": "2025-07-12T21:47:47.982681Z",
"url": "https://files.pythonhosted.org/packages/56/24/d7ebb26efe32584ca5c438c9c14aec1453fe300536edd694d9b04960cfa5/python_pgsql_parser-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-12 21:47:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yourusername",
"github_project": "python-pgsql-parser",
"github_not_found": true,
"lcname": "python-pgsql-parser"
}