Building robust backends often involves extensive coding for CRUD operations, authorization, and business logic—a time-consuming process. This article explores how Hasura and PostgreSQL streamline backend development, enabling faster app launches with minimal coding effort.
Hasura, an open-source GraphQL engine, automatically generates GraphQL and REST APIs from your database schema. It supports data modeling, real-time queries, event programming, role-based authorization, and custom business logic execution via GraphQL actions.
PostgreSQL, a powerful open-source relational database, offers enterprise-level features comparable to Oracle and Microsoft SQL Server, making it a reliable choice for large-scale applications. Its use by companies like Netflix and Spotify underscores its capabilities. While Hasura supports other databases, this article focuses on PostgreSQL due to its accessibility and open-source nature. MySQL support is planned.
This guide highlights Hasura's features for building scalable backends for web and mobile apps. It also demonstrates how PostgreSQL's capabilities, accessed via SQL, can perform complex computations and analytics, reducing the need for custom server-side code.
Key Advantages:
- Rapid Backend Development: Hasura's auto-generated APIs significantly reduce coding time.
- PostgreSQL Power: Leverage PostgreSQL's advanced features (table inheritance, concurrency control, JSON/Spatial data types) for enhanced data integrity and performance.
- Real-time Capabilities: Hasura's real-time querying, event programming, and role-based authorization create highly responsive applications.
- Easy Deployment: Deploy Hasura using Docker for local development or Hasura Cloud for scalable, secure, globally distributed applications with features like monitoring and caching.
- Comprehensive Web Console: Manage data modeling, CRUD operations, and complex queries within Hasura's intuitive web console. Integrate with authentication and file storage providers.
- Streamlined Workflows: Utilize Hasura's migration tools and environment staging for seamless transitions between development and production, supporting CI/CD.
Why Choose PostgreSQL?
Relational databases like PostgreSQL offer advantages over other database types, including schema modeling, JOIN operations, and ACID transactions—crucial for data integrity. PostgreSQL further excels with:
- Table Inheritance: Efficient data modeling.
- Advanced Concurrency Control: Handles high-volume data writes effectively in multi-user environments.
- Fault Tolerance and Data Integrity: Reduces data corruption risks.
- Unique Data Types: Supports JSON and Spatial data types, beneficial for various applications.
PostgreSQL also acts as a computation server, executing custom functions and triggers for analytical tasks, eliminating the need for separate server-side code. This is achieved using:
- Views: Simplify complex queries.
- Functions & Operators: Perform date formatting, pattern matching, and arithmetic operations.
- Extensions: Extend PostgreSQL's functionality (e.g., PostGIS).
- Procedural Languages: Write user-defined functions and stored procedures (e.g., PL/pgSQL).
Hasura exposes this PostgreSQL logic via GraphQL, making it readily accessible to frontend applications.
PostgreSQL Examples:
- Example 1 (Online Users View): A view to retrieve currently active users:
CREATE OR REPLACE VIEW "public"."online_users" AS SELECT users.id, users.last_seen FROM users WHERE (users.last_seen >= (now() - '00:00:30'::interval));
- Example 2 (Geolocation with PostGIS): Find stores within a 1000-meter radius:
SELECT id, name, address, geom FROM Seattle_Starbucks WHERE ST_DWithin(geom, ST_MakePoint(-122.325959,47.625138)::geography, 1000);
What is Hasura?
Hasura is an open-source, real-time GraphQL engine that generates APIs for your database. Its web console allows for:
- Schema Modeling: Create tables, define relationships, and manage data.
- CRUD Operations: Perform create, read, update, and delete actions.
- Role-Based Access Control: Implement granular permissions.
- GraphQL/REST Endpoint Creation: Generate APIs for your data.
- SQL Execution: Run custom SQL queries.
- Action & Trigger Definition: Automate tasks and handle events.
Note: Hasura requires integration with a separate authentication provider (e.g., Auth0, Firebase) and file storage service. NHost offers an integrated solution.
Launching Hasura:
- Docker: Recommended for local development, offering easy setup and no rate limiting.
- Hasura Cloud: Simplifies deployment with scalability, security, and global distribution, including monitoring and caching features. A free tier is available with limitations.
Hasura Features (Detailed Overview):
-
Data Manager: Visual schema designer for creating tables, defining relationships, and managing data types (including JSON and custom types).
-
Authorization: Role-based access control with granular permissions for insert, select, update, and delete operations.
-
Queries: Execute various GraphQL queries (simple, nested, aggregation, filtering) directly within the console.
- Mutations: Perform INSERT, UPSERT, UPDATE, and DELETE operations with support for transactions.
- Subscriptions: Implement real-time data updates using GraphQL subscriptions over WebSockets.
- Remote Schemas: Integrate with third-party APIs for data and logic.
- Actions: Execute custom business logic via webhooks using any programming language.
- Event Triggers: Invoke webhooks based on database events (INSERT, UPDATE, DELETE). Supports manual invocation.
-
Scheduled Triggers: Run tasks periodically (CRON) or once (one-off) via webhooks.
- Migrations and Environments: Manage schema changes using migration files for database and Hasura metadata, supporting CI/CD workflows.
Deployment Options:
- Hasura Cloud: Easiest production deployment.
- External Hosting: Heroku, Digital Ocean, Render, Azure, Kubernetes, AWS (more complex).
- NHost: All-in-one BaaS solution including PostgreSQL, Hasura, authentication, and storage.
Summary:
Hasura and PostgreSQL offer a powerful combination for rapid backend development. The reduced coding effort, real-time capabilities, and robust features make it a compelling solution for building scalable and maintainable applications. While not as feature-rich as some alternatives in every aspect, its ease of use, focus on performance, and open-source nature are significant advantages.
Frequently Asked Questions (FAQs): (The provided FAQs are already comprehensive and well-written; no modification needed.)
The above is the detailed content of How to Build Your Backend with Hasura and PostgreSQL. For more information, please follow other related articles on the PHP Chinese website!

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
