PgRoutiner是一组PostgreSQL数据库和PostgreSQL .NET项目的命令行工具。
使用您的.NET配置项目连接字符串(或自定义定义连接) - 您可以:
轻松浏览并搜索数据库。
生成C#和TS模型和代码。
生成数据库脚本和运行工具。
生成Markdown文档。
生成CRUD命令函数。
查看演示幻灯片
查看Pgroutiner博客
请参阅Pgroutiner概念博客
请参阅Pgroutiner数据库管理博客
请参阅Pgroutiner代码 - 天文博客
请参阅Pgroutiner文档博客
注意:此读取文件中的所有示例以及上面的演示文稿中使用[PostgreSQL Tutorial]的PostgreSQL示例数据库
快速开始:
- 从“版本”页面下载最新版本的本机可执行文件(不取决于任何框架)。
- 将适当的路径设置为下载的可执行文件。
- 类型
pgroutiner --info- 注意:使用本机可执行文件的速度更快,它们的启动时间很短,并且提供了很多倍更好的用户体验。
目录:
这是使用Pgroutiner工具开始的最快,最简单的方法。该版本页面包含可下载的可执行文件,这些可执行文件不取决于任何内容。不需要框架或码头,只需简单的旧本机可执行即可。
实际上,这是使用Pgroutiner工具的优选方法。本地可执行文件非常优化,启动时间很短,它们提供了多倍更好的用户体验。
这是步骤:
pgroutiner --info以查看f。就是这样。
安装全局工具(建议):
$ dotnet tool install --global dotnet-pgroutiner
Tool 'dotnet-pgroutiner' (version '5.4.0') was successfully installed.更新全局工具:
$ dotnet tool update --global dotnet-pgroutiner
Tool 'dotnet-pgroutiner' was successfully updated from version '5.0.7' to version '5.0.8'.这将启用全局命令行工具pgroutiner 。尝试键入pgroutiner --help 。
为了在项目中添加本地工具,您只需要创建一个清单文件,并如本教程中所述添加一个没有--global Switch的工具。
TL-DR:
将.config目录添加到您的项目或解决方案中。
将dotnet-tools.json文件添加到此目录中,并带有以下内容:
{
"version" : 1 ,
"isRoot" : true ,
"tools" : {
"dotnet-pgroutiner" : {
"version" : " 5.0.6 " ,
"commands" : [
" pgroutiner "
]
}
}
}从您的命令行类型dotnet tool restore
使用dotnet tool run pgroutiner [arguments] ,例如, dotnet tool run pgroutiner --help
pgroutiner (请参阅上面的说明)pgroutiner --help支持可用的命令和交换机(列表很长)。pgroutiner以定义新连接(如果您没有一个连接) - 并为此DIR创建默认配置文件。查看有关连接管理的更多信息pgroutiner --info以查看是否可以连接到数据库并查看其他环境信息。pgroutiner --list查看所有对象的列表。pgroutiner --ddl [object_name]从第二个参数中查看对象的数据定义语言。pgroutiner --search [search expression]以搜索搜索表达式搜索数据定义。 pgroutiner旨在从.NET Project root运行 - 它将从标准配置JSON文件(例如appsettings.Development.json and appsettings.json ,按此顺序读取任何可用的连接)。
它将使用ConnectionStrings部分中的第一个可用连接字符串:
AppSettings.json
{
"ConnectionStrings" : {
"DvdRental" : " Server=localhost;Db=dvdrental;Port=5432;User Id=postgres;Password=postgres; "
}
}注意:这是NPGSQL连接字符串格式,但是,它还可以使用标准的Postgresql URL连接格式postgresql://{user}:{password}@{server}:{port}/{database} 。
运行简单的信息命令测试连接以查看环境:
~$ pgroutiner --info
Version:
5.0.3.0
Executable dir:
/home/vbilopav/.dotnet/tools
OS:
Unix 5.10.102.1
Using configuration files:
appsettings.Development.json
appsettings.json
Using dir:
/home/vbilopav/dev/dvdrental
Using settings:
--info
Using connection DvdRental:
Host=localhost;Database=dvdrental;Port=5432;Username=postgres (PostgreSQL 13.8)
Using project file:
dvdrental.csproj
pg_dump:
pg_dump
pg_restore:
/usr/lib/postgresql/13/bin/pg_restore ~$ pgroutiner
Connection server [localhost]:
Connection port [5432]:
Connection database [postgres]:
Connection user [postgres]:
Connection password [environment var.]:-c或--connection参数: ~$ pgroutiner -c ConnectionString2 --info ~$ pgroutiner --connection ConnectionString2 --info Connection server [localhost]:
Connection port [5432]:
Connection database [postgres]:
Connection user [postgres]:
Connection password:连接服务器,端口,数据库和用户具有预定义的默认值( localhost , 5432 , postgres , postgres ) - 点击Enter以跳过并使用默认值。
命令行可以将整个连接字符串与-c或--connection - 而不是连接名称:
~$ pgroutiner --connection "Server=localhost;Db=test;Port=5432;User Id=postgres;Password=postgres;" --infopostgresql://{user}:{password}@{server}:{port}/{database} - 而不是npgsql Connection strint: {
"ConnectionStrings" : {
"TestConnection" : " postgresql://postgres:postgres@localhost:5432/test "
}
} ~$ pgroutiner --connection "postgresql://postgres:postgres@localhost:5432/test" --info连接(服务器,端口,数据库,用户和密码)的每个部分都可以从连接字符串或连接URL省略,并将其替换为以下环境变量:
PGHOST或PGSERVER替换缺少的服务器参数。
PGPORT替换缺失的端口参数。
PGDATABASE或PGDB替换缺少的数据库参数。
PGHOST或PGSERVER替换缺少的服务器参数。
PGUSER替换缺少的用户参数。
PGPASSWORD或PGPASS以替换丢失的密码参数。
可以在配置文件中配置每个可能的命令行选项和开关。
使用JSON配置( appsettings.json或appsettings.Development.json )中的PgRoutiner配置部分,示例:
{
"ConnectionStrings" : {
"TestConnection" : " postgresql://postgres:postgres@localhost:5432/test "
},
"PgRoutiner" : {
// pgroutiner settings
}
}如果存在配置部分PgRoutiner ,则pgroutiner将读取并应用配置设置为默认值。
使用命令行覆盖任何配置设置。
通常,任何配置设置都在命令行中具有等效性,因为基于烤肉串的sitch以两个破折号开始。示例:
SkipConnectionPrompt ,命令行: --skip-connection-promptSchemaSimilarTo ,命令行: --schema-similar-to例如,许多设置也具有快捷方式别名,例如, Connection设置可以是: -c -conn --conn -connection --connection 。
布尔设置是开关,足以在没有价值的情况下包含一个开关以打开它: pgroutiner --skip-connection-prompt
添加false或0以将其关闭: pgroutiner --skip-connection-prompt false
您可以使用--settings Switch检查当前应用的设置。 pgroutiner --settings将迫使跳过任何操作,并且只会显示所有当前设置。它还将在评论标题中显示命令行别名。
您还可以运行pgroutiner --info ,除其他外,它将显示与默认值不同的当前应用设置。
如果在任何地方都没有配置部分PgRoutiner ,并且如果您没有任何参数运行pgroutiner ,则将提供创建默认配置文件appsettings.PgRoutiner.json :json:
You don't seem to be using any available command-line commands and PgRoutiner configuration seems to be missing.
Would you like to create a custom settings file "appsettings.PgRoutiner.json" with your current values?
This settings configuration file can be used to change settings for this directory without using a command-line.
Create "appsettings.PgRoutiner.json" in this dir [Y/N]? appsettings.PgRoutiner.json (如果存在)将始终在appsettings.json和appsettings.Development.json之后加载,并覆盖这些文件中的任何可能的设置值。
如果您需要从其他位置加载这些配置文件,则可以使用--config-path, for example, pgroutiner -config-path ../../dir2/
您还可以使用这些选项中的任何一个来加载自定义配置文件: -cf , --cf , -config , --config ,-config, -config-file , --config-file ,例如, pgroutiner --config ../../dir2/my-config.json
您还可以通过使用以下任何选项-wcf , --write-config-file pgroutiner --write-config-file ../../dir2/my-config.json当前设置写入自定义配置
该当前版本的所有默认值的完整配置文件
描述: -l -ls --ls --list to dump or search object list to console. Use the switch to dump all objects or parameter values to search.
示例:
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --list
SCHEMA public
EXTENSION plpgsql
TYPE mpaa_rating
DOMAIN public.year
TABLE public.actor
VIEW public.actor_info
VIEW public.customer_list
... vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner -l
SCHEMA public
EXTENSION plpgsql
TYPE mpaa_rating
DOMAIN public.year
TABLE public.actor
VIEW public.actor_info
VIEW public.customer_list
... vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner -ls
SCHEMA public
EXTENSION plpgsql
TYPE mpaa_rating
DOMAIN public.year
TABLE public.actor
VIEW public.actor_info
VIEW public.customer_list
... vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --list film
VIEW public.film_list
VIEW public.nicer_but_slower_film_list
VIEW public.sales_by_film_category
TABLE public.film_actor
TABLE public.film_category
TABLE public.film
SEQ public.film_film_id_seq
FUNCTION public.film_in_stock(integer, integer)
FUNCTION public.film_not_in_stock(integer, integer)
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --list actor
TABLE public.actor
VIEW public.actor_info
TABLE public.film_actor
SEQ public.actor_actor_id_seq
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $描述: -def -ddl --ddl -definition --definition to dump object schema definition in console supplied as a value parameter.
示例:
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner -ddl actor
--
-- Table: public.actor
--
CREATE TABLE public.actor (
actor_id integer DEFAULT nextval('public.actor_actor_id_seq'::regclass) NOT NULL PRIMARY KEY,
first_name character varying(45) NOT NULL,
last_name character varying(45) NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.actor OWNER TO postgres;
CREATE INDEX idx_actor_last_name ON public.actor USING btree (last_name);
CREATE TRIGGER last_updated BEFORE UPDATE ON public.actor FOR EACH ROW EXECUTE FUNCTION public.last_updated();
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner -ddl film
--
-- Table: public.film
--
CREATE TABLE public.film (
film_id integer DEFAULT nextval('public.film_film_id_seq'::regclass) NOT NULL PRIMARY KEY,
title character varying(255) NOT NULL,
description text,
release_year public.year,
language_id smallint NOT NULL,
rental_duration smallint DEFAULT 3 NOT NULL,
rental_rate numeric(4,2) DEFAULT 4.99 NOT NULL,
length smallint,
replacement_cost numeric(5,2) DEFAULT 19.99 NOT NULL,
rating public.mpaa_rating DEFAULT 'G'::public.mpaa_rating,
last_update timestamp without time zone DEFAULT now() NOT NULL,
special_features text[],
fulltext tsvector NOT NULL,
FOREIGN KEY (language_id) REFERENCES public.language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT
);
ALTER TABLE public.film OWNER TO postgres;
CREATE INDEX film_fulltext_idx ON public.film USING gist (fulltext);
CREATE INDEX idx_fk_language_id ON public.film USING btree (language_id);
CREATE INDEX idx_title ON public.film USING btree (title);
CREATE TRIGGER film_fulltext_trigger BEFORE INSERT OR UPDATE ON public.film FOR EACH ROW EXECUTE FUNCTION tsvector_update_trigger('fulltext', 'pg_catalog.english', 'title', 'description');
CREATE TRIGGER last_updated BEFORE UPDATE ON public.film FOR EACH ROW EXECUTE FUNCTION public.last_updated();
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --ddl film_in_stock
--
-- Function: public.film_in_stock
--
CREATE FUNCTION public.film_in_stock(
p_film_id integer,
p_store_id integer,
OUT p_film_count integer
)
RETURNS SETOF integer
LANGUAGE sql
AS $_$
SELECT inventory_id
FROM inventory
WHERE film_id = $1
AND store_id = $2
AND inventory_in_stock(inventory_id);
$_$;
ALTER FUNCTION public.film_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres;
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --ddl " actor;film_in_stock "
--
-- Table: public.actor
--
CREATE TABLE public.actor (
actor_id integer DEFAULT nextval('public.actor_actor_id_seq'::regclass) NOT NULL PRIMARY KEY,
first_name character varying(45) NOT NULL,
last_name character varying(45) NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.actor OWNER TO postgres;
CREATE INDEX idx_actor_last_name ON public.actor USING btree (last_name);
CREATE TRIGGER last_updated BEFORE UPDATE ON public.actor FOR EACH ROW EXECUTE FUNCTION public.last_updated();
--
-- Function: public.film_in_stock
--
CREATE FUNCTION public.film_in_stock(
p_film_id integer,
p_store_id integer,
OUT p_film_count integer
)
RETURNS SETOF integer
LANGUAGE sql
AS $_$
SELECT inventory_id
FROM inventory
WHERE film_id = $1
AND store_id = $2
AND inventory_in_stock(inventory_id);
$_$;
ALTER FUNCTION public.film_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres;
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $描述: s --s --search to search object schema definitions and dump highlighted results to the console.
搜索整个定义和转储以控制包含表达式的整个定义。
注意:突出显示搜索表达式匹配。
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --search " select public.group_concat "
--
-- View: public.actor_info
--
CREATE VIEW public.actor_info AS
SELECT a.actor_id,
a.first_name,
a.last_name,
public.group_concat(DISTINCT (((c.name)::text || ': '::text) || ( SELECT public.group_concat((f.title)::text) AS group_concat
FROM ((public.film f
JOIN public.film_category fc_1 ON ((f.film_id = fc_1.film_id)))
JOIN public.film_actor fa_1 ON ((f.film_id = fa_1.film_id)))
WHERE ((fc_1.category_id = c.category_id) AND (fa_1.actor_id = a.actor_id))
GROUP BY fa_1.actor_id))) AS film_info
FROM (((public.actor a
LEFT JOIN public.film_actor fa ON ((a.actor_id = fa.actor_id)))
LEFT JOIN public.film_category fc ON ((fa.film_id = fc.film_id)))
LEFT JOIN public.category c ON ((fc.category_id = c.category_id)))
GROUP BY a.actor_id, a.first_name, a.last_name;
ALTER TABLE public.actor_info OWNER TO postgres;
vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ vbilopav@DESKTOP-O3A6QK2:~/dev/dvdrental $ pgroutiner --search smallint
--
-- Table: public.store
--
CREATE TABLE public.store (
store_id integer DEFAULT nextval('public.store_store_id_seq'::regclass) NOT NULL PRIMARY KEY,
manager_staff_id smallint NOT NULL,
address_id smallint NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL,
FOREIGN KEY (address_id) REFERENCES public.address(address_id) ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (manager_staff_id) REFERENCES public.staff(staff_id) ON UPDATE CASCADE ON DELETE RESTRICT
);
ALTER TABLE public.store OWNER TO postgres;
CREATE UNIQUE INDEX idx_unq_manager_staff_id ON public.store USING btree (manager_staff_id);
CREATE TRIGGER last_updated BEFORE UPDATE ON public.store FOR EACH ROW EXECUTE FUNCTION public.last_updated();
--
-- Table: public.address
--
CREATE TABLE public.address (
address_id integer DEFAULT nextval('public.address_address_id_seq'::regclass) NOT NULL PRIMARY KEY,
address character varying(50) NOT NULL,
address2 character varying(50),
district character varying(20) NOT NULL,
city_id smallint NOT NULL,
postal_code character varying(10),
phone character varying(20) NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL,
CONSTRAINT fk_address_city FOREIGN KEY (city_id) REFERENCES public.city(city_id)
);
...根据命令,此工具将使用PostgreSQL客户端工具(例如psql , pg_dump或pg_restore启动外部进程。
这意味着,必须在系统上安装PostgreSQL客户端工具。默认情况下,每个PostgreSQL安装都将安装PostgreSQL客户端工具。
如果您不想要服务器,而只需要客户端工具:
postgresql-client就足够了,例如$ sudo apt-get install -y postgresql-client ,但取决于系统。当pgroutiner调用外部工具时,它将首先尝试调用默认别名psql或pg_dump 。然后,如果该工具的版本与连接中的版本不匹配,它将尝试在默认位置找到可执行文件:
C:Program FilesPostgreSQL{0}binpg_dump.exe和C:Program FilesPostgreSQL{0}binpsql.exe用于Windows Systems。/usr/lib/postgresql/{0}/bin/pg_dump和/usr/lib/postgresql/{0}/bin/psql用于Linux Systems。{0}是主要版本号。这些路径默认情况下是postgresql安装二进制文件。
当Pgroutiner看到版本不匹配时,它将引起警告并带有适当的版本号到该路径的后备。
可以分别通过设置PgDumpFallback和PsqlFallback设置值来避免这种行为。
这是开发和维护的开源软件,而无需任何薪酬。
版权所有(C)VedranBilopavlović -VB咨询和VB软件2020此源代码已根据MIT许可获得许可。