{"id":4189,"date":"2017-05-08T11:55:38","date_gmt":"2017-05-08T02:55:38","guid":{"rendered":"https:\/\/kumapress.com\/wp\/?p=4189"},"modified":"2017-06-15T19:17:25","modified_gmt":"2017-06-15T10:17:25","slug":"node-js%e3%81%8b%e3%82%89postgresql%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%be%e3%81%97%e3%81%9f","status":"publish","type":"post","link":"https:\/\/kumapress.com\/?p=4189","title":{"rendered":"node.js\u304b\u3089Postgresql\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f"},"content":{"rendered":"<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">\u666e\u6bb5DB\u306fPostgresql\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">MySQL\u306f\u307b\u307cWordPress\u306e\u3068\u304d\u306e\u307f(^^;<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">\u4eca\u56de\u306fnode.js\u304b\u3089PostgreSQL\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/span><\/p>\n<h2>\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/h2>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">$ npm install pg<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> exapp@0.0.0 \/home\/vagrant\/nodej\/exapp<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2514\u2500\u252c pg@6.1.5<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u2500 buffer-writer@1.0.1<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u2500 packet-reader@0.2.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u2500 pg-connection-string@0.1.3<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u252c pg-pool@1.7.1<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u251c\u2500\u2500 generic-pool@2.4.3<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u2514\u2500\u2500 object-assign@4.1.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u252c pg-types@1.11.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u251c\u2500\u2500 ap@0.2.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u251c\u2500\u2500 postgres-array@1.0.2<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u251c\u2500\u2500 postgres-bytea@1.0.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u251c\u2500\u2500 postgres-date@1.0.3<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u2514\u2500\u252c postgres-interval@1.0.2<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u2514\u2500\u2500 xtend@4.0.1<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u251c\u2500\u252c pgpass@1.0.1<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u2514\u2500\u252c split@1.0.0<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2502 \u2514\u2500\u2500 through@2.3.8<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> \u2514\u2500\u2500 semver@4.3.2<\/span><\/p>\n<h2>\u30c6\u30b9\u30c8\u7528\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/h2>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"><strong>-bash-4.2$ createuser -P datauser<\/strong><\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Enter password for new role: datapass<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Enter it again:<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Password: postgres\u306e\u30d1\u30b9\u30ef\u30fc\u30c9<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">postgres=# \\du<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> List of roles<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Role name | Attributes | Member of<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> &#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8211;<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> datauser | | {}<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"><strong>-bash-4.2$ createdb test_db -O datauser<\/strong><\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Password:<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> <strong>-bash-4.2$ psql -l<\/strong><\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Password:<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> List of databases<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Name | Owner | Encoding | Collate | Ctype | Access privileges<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> &#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> postgres | postgres | UTF8 | C | C |<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> tdetestdb | postgres | UTF8 | C | C |<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> template0 | postgres | UTF8 | C | C | =c\/postgres +<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> | | | | | postgres=CTc\/postgres<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> template1 | postgres | UTF8 | C | C | =c\/postgres +<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> | | | | | postgres=CTc\/postgres<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db | datauser | UTF8 | C | C |<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> (5 rows)<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"><strong>-bash-4.2$ psql -U datauser test_db<\/strong><\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Password for user datauser:<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db=&gt; create table mydata (<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db(&gt; id serial primary key,<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db(&gt; name varchar(50),<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db(&gt; mail varchar(100),<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db(&gt; memo varchar(255)<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db(&gt; );<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> CREATE TABLE<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> test_db=&gt; \\dp<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Access privileges<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Schema | Name | Type | Access privileges | Column privileges | Policies<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> &#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> public | mydata | table | | |<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> public | mydata_id_seq | sequence | | |<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> (2 rows)<\/span><\/p>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">test_db=&gt; \\d<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> List of relations<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> Schema | Name | Type | Owner<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> &#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> public | mydata | table | datauser<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> public | mydata_id_seq | sequence | datauser<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> (2 rows)<\/span><\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u3067\u3059<\/h2>\n<p>create.js<\/p>\n<pre>  var express = require('express');\r\n  var router = express.Router();\r\n  var pg = require('pg');\r\n\r\n  \/* create page *\/\r\n  router.post( '\/', function(req, res, next) {\r\n    var name_str = req.body[\"name\"];\r\n    var mail_str = req.body[\"mail\"];\r\n    var memo_str = req.body[\"memo\"];\r\n\r\n    var con = \"tcp:\/\/datauser:datapass@localhost:5432\/test_db\";\r\n    pg.connect( con, function( err, client ) {\r\n      var qstr = \"insert into mydata( name, mail, memo ) values ( $1, $2, $3 )\";\r\n      var query = client.query( qstr, [name_str, mail_str, memo_str]);\r\n      query.on( 'end', function( row, err ) {\r\n        res.redirect( \"\/\" );\r\n      });\r\n      query.on( 'error', function( error ) {\r\n        console.log( \"ERROR!\" );\r\n        res.render( 'index', {\r\n          title: \"ERROR\",\r\n          data: null,\r\n          message: \"ERROR is occured!\"\r\n        });\r\n      });\r\n    });\r\n  });\r\n\r\nmodule.exports = router;\r\n<\/pre>\n<p><span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\">SQL\u3092\u305d\u306e\u307e\u307e\u4f7f\u3048\u308b\u306e\u304c\u3042\u308a\u304c\u305f\u3044\u304b\u3082\u3067\u3059(^^;<\/span><br \/>\n<span style=\"font-size: 14pt; font-family: arial, helvetica, sans-serif;\"> OR\u30de\u30c3\u30d1\u30fc\u306f\u3042\u308b\u306e\u304b\u306a\uff1f\uff1f\uff1f<\/span><\/p>\n<aside class=\"row veu_insertAds after\"><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 2\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"5379270693\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 1\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"7353370294\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><\/aside>","protected":false},"excerpt":{"rendered":"<p>\u666e\u6bb5DB\u306fPostgresql\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002 MySQL\u306f\u307b\u307cWordPress\u306e\u3068\u304d\u306e\u307f(^^; \u4eca\u56de\u306fnode.js\u304b\u3089PostgreSQL\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002 $ npm i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[143,126],"tags":[],"class_list":["post-4189","post","type-post","status-publish","format-standard","hentry","category-node-js","category-blog-memo"],"_links":{"self":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4189"}],"version-history":[{"count":6,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4189\/revisions"}],"predecessor-version":[{"id":4192,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4189\/revisions\/4192"}],"wp:attachment":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}