postgres
Mirror of the official PostgreSQL GIT repository. Note that this is just a *mirror* - we don't work with pull requests on github. To contribute, please see https://wiki.postgresql.org/wiki/Submitting_a_Patch
Top Related Projects
CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
MySQL Server, the world's most popular open source database, and MySQL Cluster, a real-time, open source transactional database.
MariaDB server is a community developed fork of MySQL server. Started by core members of the original MySQL team, MariaDB actively works with outside developers to deliver the most featureful, stable, and sanely licensed open SQL server in the industry.
YugabyteDB - the cloud native distributed SQL database for mission-critical applications.
Distributed PostgreSQL as an extension
TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
Quick Overview
PostgreSQL, often referred to as Postgres, is a powerful, open-source object-relational database system. It is known for its reliability, feature robustness, and performance. PostgreSQL extends the SQL language combined with many features that safely store and scale complex data workloads.
Pros
- Highly extensible with custom functions, operators, and data types
- Strong support for concurrent users and heavy workloads
- Advanced features like Multi-Version Concurrency Control (MVCC) and point-in-time recovery
- Excellent compliance with SQL standards
Cons
- Can be complex to set up and configure for optimal performance
- Slower than some other databases for simple read-heavy operations
- Requires more system resources compared to lighter database solutions
- Less widespread adoption compared to MySQL, potentially leading to a smaller community and fewer third-party tools
Code Examples
- Creating a table and inserting data:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
- Performing a complex query with joins and aggregations:
SELECT
departments.name AS department,
COUNT(employees.id) AS employee_count,
AVG(employees.salary) AS avg_salary
FROM
departments
LEFT JOIN
employees ON departments.id = employees.department_id
GROUP BY
departments.id
HAVING
COUNT(employees.id) > 5
ORDER BY
avg_salary DESC;
- Using a PostgreSQL-specific feature (JSON operations):
CREATE TABLE events (
id SERIAL PRIMARY KEY,
data JSONB
);
INSERT INTO events (data) VALUES ('{"user": "Alice", "action": "login", "timestamp": "2023-04-15T10:30:00Z"}');
SELECT data->'user' AS username, data->'action' AS action
FROM events
WHERE data @> '{"action": "login"}';
Getting Started
To get started with PostgreSQL:
- Install PostgreSQL on your system (varies by OS)
- Start the PostgreSQL service
- Connect to the database using psql or a GUI tool
- Create a new database:
CREATE DATABASE mydb;
- Connect to the new database:
\c mydb
- Create tables and start using PostgreSQL:
CREATE TABLE example (id SERIAL PRIMARY KEY, name VARCHAR(100)); INSERT INTO example (name) VALUES ('Hello, PostgreSQL!'); SELECT * FROM example;
Competitor Comparisons
CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
Pros of CockroachDB
- Designed for horizontal scalability and distributed operations
- Built-in support for multi-region deployments and geo-partitioning
- Strong consistency model with serializable isolation by default
Cons of CockroachDB
- Less mature ecosystem and community compared to PostgreSQL
- Some PostgreSQL features and extensions are not supported
- Higher resource consumption for small-scale deployments
Code Comparison
PostgreSQL:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CockroachDB:
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name STRING,
created_at TIMESTAMP DEFAULT current_timestamp()
);
The main differences in this example are:
- CockroachDB uses
UUID
as the default primary key type instead ofSERIAL
. - CockroachDB uses
STRING
instead ofTEXT
for string data. - The
gen_random_uuid()
function is used to generate unique IDs in CockroachDB.
Both databases support SQL syntax, but CockroachDB has some unique features and syntax differences to accommodate its distributed nature and scalability focus. While PostgreSQL is a more established and feature-rich database, CockroachDB offers advantages in terms of horizontal scaling and distributed operations, making it suitable for certain use cases where these features are critical.
MySQL Server, the world's most popular open source database, and MySQL Cluster, a real-time, open source transactional database.
Pros of MySQL
- Generally faster for read-heavy workloads and simple queries
- Easier to set up and manage for beginners
- More widespread adoption, especially in web applications
Cons of MySQL
- Less ACID-compliant in certain configurations
- Limited support for complex queries and advanced features
- Less adherence to SQL standards compared to PostgreSQL
Code Comparison
MySQL:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
PostgreSQL:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
The main difference in this example is the use of AUTO_INCREMENT
in MySQL versus SERIAL
in PostgreSQL for creating an auto-incrementing primary key. Both achieve similar results, but PostgreSQL's SERIAL
is more standards-compliant.
MySQL also tends to be more lenient with data types, while PostgreSQL is stricter. For instance, MySQL might allow implicit type conversions that PostgreSQL would reject, requiring more explicit casting.
Overall, both databases have their strengths and are suitable for different use cases. MySQL excels in simple, read-heavy scenarios, while PostgreSQL offers more advanced features and better standards compliance.
MariaDB server is a community developed fork of MySQL server. Started by core members of the original MySQL team, MariaDB actively works with outside developers to deliver the most featureful, stable, and sanely licensed open SQL server in the industry.
Pros of MariaDB server
- More storage engines, including specialized ones like ColumnStore for analytics
- Galera Cluster for multi-master replication out-of-the-box
- Generally faster performance for write-intensive workloads
Cons of MariaDB server
- Smaller community and ecosystem compared to Postgres
- Less advanced support for JSON and other NoSQL-like features
- Some compatibility issues with MySQL, despite being a fork
Code comparison
MariaDB server (SQL syntax):
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
Postgres (SQL syntax):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
The main difference in this example is the AUTO_INCREMENT
vs SERIAL
for auto-incrementing primary keys, and the explicit engine specification in MariaDB. Postgres uses SERIAL
as a shorthand for an auto-incrementing integer column.
Both projects are open-source relational database management systems with active development. MariaDB offers some unique features and potentially better performance for certain workloads, while Postgres has a larger community and more advanced features in some areas. The choice between them often depends on specific project requirements and existing infrastructure.
YugabyteDB - the cloud native distributed SQL database for mission-critical applications.
Pros of YugabyteDB
- Distributed architecture for high availability and horizontal scalability
- Built-in support for multi-region deployments and geo-distribution
- ACID-compliant transactions across multiple nodes and regions
Cons of YugabyteDB
- Relatively newer project with a smaller community compared to Postgres
- Some advanced Postgres features may not be fully supported or implemented
- Potential learning curve for teams familiar with traditional RDBMS systems
Code Comparison
YugabyteDB extends Postgres syntax with additional features:
-- YugabyteDB: Creating a table with hash-based sharding
CREATE TABLE users (
id INT PRIMARY KEY,
name TEXT
) SPLIT INTO 4 TABLETS;
-- Postgres: Standard table creation
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);
YugabyteDB supports distributed transactions across nodes:
-- YugabyteDB: Multi-node transaction
BEGIN TRANSACTION;
INSERT INTO users (id, name) VALUES (1, 'Alice') ON TABLET 1;
INSERT INTO users (id, name) VALUES (2, 'Bob') ON TABLET 2;
COMMIT;
-- Postgres: Standard transaction
BEGIN;
INSERT INTO users (name) VALUES ('Alice');
INSERT INTO users (name) VALUES ('Bob');
COMMIT;
Both projects use C for core database functionality, but YugabyteDB incorporates additional languages like C++ for its distributed components.
Distributed PostgreSQL as an extension
Pros of Citus
- Enables horizontal scaling of PostgreSQL for improved performance on large datasets
- Provides built-in sharding capabilities for distributed query processing
- Offers real-time analytics on large-scale data with minimal latency
Cons of Citus
- Adds complexity to database management and maintenance
- May require application changes to fully leverage distributed capabilities
- Limited compatibility with some PostgreSQL extensions and features
Code Comparison
PostgreSQL (standard query):
SELECT * FROM users WHERE city = 'New York';
Citus (distributed query):
SELECT * FROM users_distributed WHERE city = 'New York';
The main difference is that Citus requires tables to be distributed across nodes, which is reflected in the table name and underlying query execution.
Key Differences
- Postgres is a traditional relational database, while Citus extends Postgres for distributed computing
- Citus focuses on scalability and performance for large-scale applications
- Postgres offers a wider range of features and extensions, while Citus specializes in distributed workloads
Use Cases
- Postgres: General-purpose database for various application types
- Citus: High-performance analytics, real-time dashboards, and large-scale multi-tenant applications
TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
Pros of TiDB
- Designed for horizontal scalability and distributed architecture
- Supports both OLTP and OLAP workloads
- Compatible with MySQL protocol, making migration easier
Cons of TiDB
- Relatively newer project with a smaller community compared to Postgres
- May have fewer extensions and third-party tools available
- Performance can be less predictable in certain scenarios due to distributed nature
Code Comparison
TiDB (SQL syntax):
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255),
created_at TIMESTAMP
);
Postgres (SQL syntax):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
created_at TIMESTAMP
);
The SQL syntax for basic operations is similar between TiDB and Postgres, with minor differences in data types and default behaviors. TiDB aims for MySQL compatibility, while Postgres follows the SQL standard more closely.
TiDB is built for distributed scenarios and horizontal scaling, making it suitable for large-scale applications with high concurrency. Postgres excels in traditional relational database use cases and offers a more mature ecosystem.
Both projects are open-source and actively maintained, with TiDB focusing on distributed SQL capabilities and Postgres continuing to enhance its robust feature set and performance for single-node deployments.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.
Top Related Projects
CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
MySQL Server, the world's most popular open source database, and MySQL Cluster, a real-time, open source transactional database.
MariaDB server is a community developed fork of MySQL server. Started by core members of the original MySQL team, MariaDB actively works with outside developers to deliver the most featureful, stable, and sanely licensed open SQL server in the industry.
YugabyteDB - the cloud native distributed SQL database for mission-critical applications.
Distributed PostgreSQL as an extension
TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot