NpgsqlRest is an Automatic REST API and Code Generator for the PostgreSQL Databases.
9.2K
Automatic PostgreSQL Web Server
Transform your PostgreSQL database into a production-ready, blazing-fast REST API Standalone Web Server.
Generate code, build entire applications and more.
NpgsqlRest is the superior alternative to existing automatic PostgreSQL REST API solutions, offering unmatched performance and advanced enterprise features.
Download, configure, and run - your REST API server is live in seconds with comprehensive enterprise features built-in.
RAISE INFO statements. No database locking.And more!
Starting is easy:
appsettings.jsonLet's create a simple Hello World function and add a simple comment annotation:
create function hello_world()
returns text
language sql
as $$
select 'Hello World'
$$;
comment on function hello_world() is '
HTTP GET /hello
authorize admin
';
This annotation will create an HTTP GET /hello endpoint that returns "Hello World" and will authorize only the admin role.
We could also add any HTTP response header, like for example Content-Type: text/plain, but since this function returns text, the response will be text/plain anyhow.
Note: Anything that is not a valid HTTP header or a comment annotation that alters behavior will be ignored and treated as a function comment.
You have a choice to do the best approach that suits you. Either one of these things:
Download the appropriate executable for your target OS from Releases page. You can use manual download, wget, or anything you want. Just remember to assign appropriate executable permissions after the download.
~/dev
❯ npm i npgsqlrest
added 1 package in 31s
Note: NPM package will do the same thing on install automatically: Download the appropriate executable for your target OS from Releases page.
~/dev
❯ docker pull vbilopav/npgsqlrest:latest
latest: Pulling from vbilopav/npgsqlrest
Digest: sha256:70b4057343457e019657dca303acbed8a1acd5f83075ea996b8e6ea20dac4b48
Status: Image is up to date for vbilopav/npgsqlrest:latest
docker.io/vbilopav/npgsqlrest:latest
~/dev
Minimal Configuration is appsettings.json file with one connection string. You can do that with any editor or, using bash:
~/dev
❯ cat > appsettings.json << EOF
{
"ConnectionStrings": {
"Default": "Host=localhost;Port=5432;Database=my_db;Username=postgres;Password=postgres"
}
}
EOF
Type executable name:
~/dev
❯ ./npgsqlrest
[11:33:35.440 INF] Started in 00:00:00.0940095, listening on ["http://localhost:8080"], version 2.26.0.0 [NpgsqlRest]
Or, run as NPX command for NPM distributions:
~/dev
❯ npx npgsqlrest
[11:33:35.440 INF] Started in 00:00:00.0940095, listening on ["http://localhost:8080"], version 2.26.0.0 [NpgsqlRest]
Or, run the appropriate Docker command (expose the 8080 default port and bind the default configuration):
~/dev
❯ docker run -p 8080:8080 -v ./appsettings.json:/app/appsettings.json --network host vbilopav/npgsqlrest:latest
[11:33:35.440 INF] Started in 00:00:00.0940095, listening on ["http://localhost:8080"], version 2.26.0.0 [NpgsqlRest]
Congratulations, your High Speed Web Server is running with /hello endpoint exposed.
Now that we have our server up and running, we can add some more configuration to make things interesting:
HttpFileOptions for the HttpFile plugin that generates HTTP files for testing.ClientCodeGen for TsClient plugin that generates TypeScript code for us.The configuration file should look like this:
{
"ConnectionStrings": {
"Default": "Host=localhost;Port=5432;Database=my_db;Username=postgres;Password=postgres"
},
"Log": {
"MinimalLevels": {
"NpgsqlRest": "Debug"
}
},
"NpgsqlRest": {
"HttpFileOptions": {
"Enabled": true,
"NamePattern": "./src/http/{0}_{1}"
},
"ClientCodeGen": {
"Enabled": true,
"FilePath": "./src/app/api/{0}Api.ts"
}
}
}
After running with this configuration, we will see much more information in the console:
~/dev
❯ ./npgsqlrest
[12:46:05.120 DBG] ----> Starting with configuration(s): JsonConfigurationProvider for 'appsettings.json' (Optional), JsonConfigurationProvider for 'appsettings.Development.json' (Optional), CommandLineConfigurationProvider [NpgsqlRest]
[12:46:05.135 DBG] Using main connection string: Host=127.0.0.1;Database=my_db;Username=postgres;Password=******;Application Name=dev;Enlist=False;No Reset On Close=True [NpgsqlRest]
[12:46:05.149 DBG] Attempting to open PostgreSQL connection (attempt 1/7) [NpgsqlRest]
[12:46:05.194 DBG] Successfully opened PostgreSQL connection on attempt 1 [NpgsqlRest]
[12:46:05.199 DBG] Using Data Protection for application dev with default provider. Expiration in 90 days. [NpgsqlRest]
[12:46:05.214 DBG] Using RoutineSource PostgreSQL Source [NpgsqlRest]
[12:46:05.215 DBG] Using CrudSource PostgreSQL Source [NpgsqlRest]
[12:46:05.309 DBG] Function public.hello_world mapped to POST /api/hello-world has set HTTP by the comment annotation to GET /hello [NpgsqlRest]
[12:46:05.311 DBG] Created endpoint GET /hello [NpgsqlRest]
[12:46:05.332 DBG] Created HTTP file: ./src/http/todo_public.http [NpgsqlRest.HttpFiles]
[12:46:05.340 DBG] Created Typescript type file: ./src/app/api/publicApiTypes.d.ts [NpgsqlRest.TsClient]
[12:46:05.340 DBG] Created Typescript file: ./src/app/api/publicApi.ts [NpgsqlRest.TsClient]
[12:46:05.358 INF] Started in 00:00:00.2759846, listening on ["http://localhost:8080"], version 2.26.0.0 [NpgsqlRest]
Also, two more files will be generated on startup:
todo_public.httpHTTP file for testing, debugging, and development (VS Code requires rest-client plugin).
@host=http://localhost:8080
// function public.hello_world()
// returns text
//
// comment on function public.hello_world is 'HTTP GET /hello
// authorize admin';
GET {{host}}/hello
###
publicApi.tsTypeScript fetch module that you can import and use in your Frontend project immediately.
// autogenerated at 2025-08-17T11:06:58.6605710+02:00
const baseUrl = "http://localhost:8080";
export const publicHelloWorldUrl = () => baseUrl + "/hello";
/**
* function public.hello_world()
* returns text
*
* @remarks
* comment on function public.hello_world is 'HTTP GET /hello
* authorize admin';
*
* @returns {{status: number, response: string}}
*
* @see FUNCTION public.hello_world
*/
export async function publicHelloWorld() : Promise<{status: number, response: string}> {
const response = await fetch(publicHelloWorldUrl(), {
method: "GET",
});
return {
status: response.status,
response: await response.text()
};
}
For a full list of configuration options, see the default configuration file. Any settings your configuration file will override these defaults.
Also, you can override these settings with console parameters. For example,e to enable Debug Level for NpgsqlRest run:
~/dev
❯ ./npgsqlrest --log:minimallevels:npgsqlrest=debug
...
And finally, to see all command line options, use -h or --help:
~/dev
❯ ./npgsqlrest --help
Usage:
npgsqlrest Run with the optional default configuration files: appsettings.json and appsettings.Development.json. If these file are not found, default configuration setting is used (see
https://github.com/NpgsqlRest/NpgsqlRest/blob/master/NpgsqlRestClient/appsettings.json).
npgsqlrest [files...] Run with the custom configuration files. All configuration files are required. Any configuration values will override default values in order of appearance.
npgsqlrest [file1 -o file2...] Use the -o switch to mark the next configuration file as optional. The first file after the -o switch is optional.
npgsqlrest [file1 --optional file2...] Use --optional switch to mark the next configuration file as optional. The first file after the --optional switch is optional.
Note: Values in the later file will override the values in the previous one.
npgsqlrest [--key=value] Override the configuration with this key with a new value (case insensitive, use : to separate sections).
npgsqlrest -v, --version Show version information.
npgsqlrest -h, --help Show command line help.
Examples:
Example: use two config files npgsqlrest appsettings.json appsettings.Development.json
Example: second config file optional npgsqlrest appsettings.json -o appsettings.Development.json
Example: override ApplicationName config npgsqlrest --applicationname=Test
Example: override Auth:CookieName config npgsqlrest --auth:cookiename=Test
...
For integrating into existing .NET applications:
dotnet add package NpgsqlRest
Note: PostgreSQL 13 minimal version is required to run the initial query to get the list of functions. The source code of this query can be found here. For versions prior to version 13, this query can be replaced with a custom query that can run on older versions.
Contributions from the community are welcome. Please make a pull request with a description if you wish to contribute.
This project is licensed under the terms of the MIT license.
Content type
Image
Digest
sha256:f07577ba1…
Size
57.5 MB
Last updated
3 months ago
docker pull vbilopav/npgsqlrest