{"id":542,"date":"2013-10-31T15:47:37","date_gmt":"2013-10-31T09:47:37","guid":{"rendered":"http:\/\/alexeyka.zantsev.com\/?p=542"},"modified":"2015-07-31T12:32:45","modified_gmt":"2015-07-31T07:32:45","slug":"postgresql-notes","status":"publish","type":"post","link":"https:\/\/alexeyka.zantsev.com\/?p=542","title":{"rendered":"PostgreSQL notes"},"content":{"rendered":"<p>1. Edit postgresql.conf line <code>listen_addresses = '10.14.1.21'<\/code> to your appropriate value.<br \/>\n2. Add to pg_hba.conf a line, which will allow connection to the database from certain IP address\/subnet: <code>host\tjohnsdb \tjohn\t10.14.1.201\/32\t\tmd5<\/code><br \/>\n3. <code>service postgresql reload<\/code><br \/>\n4. As root, &#8216;<code>su - postgres<\/code>&#8216; , then &#8216;<code>psql<\/code>&#8216; , then:<\/p>\n<p>4.1. <strong>postgres=#<\/strong> <code>CREATE USER john WITH ENCRYPTED PASSWORD 'johnstailislong' ;<\/code><br \/>\n4.2. <strong>postgres=#<\/strong> <code>CREATE DATABASE johnsdb WITH OWNER john ;<\/code><br \/>\n ( If the database owner is wrong, change it: <strong>postgres=#<\/strong> <code>ALTER DATABASE johnsdb OWNER TO john ;<\/code> )<br \/>\n5. Then connect from outside to the already created database and create a table inside it:<br \/>\n<code>psql -d johnsdb -U john -h 10.14.1.21<\/code><br \/>\n<strong>johnsdb=> <\/strong><code>CREATE TABLE johnstable (type varchar(6),name varchar(128),secret varchar(128),context varchar(128),host varchar(128)) WITH (OIDS=FALSE);<\/code><\/p>\n<p>Hints:<\/p>\n<p>Change DB&#8217;s owner (as postgresql user):<br \/>\n<code>ALTER DATABASE johnsdb OWNER TO john ;<\/code><\/p>\n<p>Change table&#8217;s owner (as postgresql user):<br \/>\n<code>ALTER TABLE johnstable OWNER TO john ;<\/code><\/p>\n<p>Table permissions: (as postgresql user):<br \/>\n<code>GRANT ALL ON TABLE johnstable TO john ;<\/code><\/p>\n<p>Change default values in a column:<br \/>\n<code>ALTER TABLE tablename ALTER COLUMN columnname SET DEFAULT 'green,red' ;<\/code><\/p>\n<p>Rename a column:<br \/>\n<code>ALTER TABLE tablename RENAME COLUMN oldcolname TO newcolname;<\/code><\/p>\n<p>Change column type:<br \/>\n<code>ALTER TABLE tablename ALTER COLUMN columnname TYPE varchar(30);<\/code><\/p>\n<p>Get last N rows:<br \/>\n<code>SELECT * FROM mytable ORDER BY record_date DESC LIMIT 5;<\/code><\/p>\n<p>Get first N rows:<br \/>\n<code>SELECT * FROM mytable ORDER BY record_date ASC LIMIT 5;<\/code><\/p>\n<p>Select between X and Y values:<br \/>\n<code>SELECT name,callerid FROM sip_conf WHERE name BETWEEN '1301' and '1310' ORDER BY name;<\/code><\/p>\n<p>Show databases:<\/p>\n<pre>\r\nastertest=> \\list\r\n                                  List of databases\r\n   Name    |   Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   \r\n-----------+-----------+----------+-------------+-------------+-----------------------\r\n astertest | astertest | UTF8     | ru_RU.UTF-8 | ru_RU.UTF-8 | \r\n postgres  | postgres  | UTF8     | ru_RU.UTF-8 | ru_RU.UTF-8 | \r\n template0 | postgres  | UTF8     | ru_RU.UTF-8 | ru_RU.UTF-8 | =c\/postgres          +\r\n           |           |          |             |             | postgres=CTc\/postgres\r\n template1 | postgres  | UTF8     | ru_RU.UTF-8 | ru_RU.UTF-8 | =c\/postgres          +\r\n           |           |          |             |             | postgres=CTc\/postgres\r\n(4 rows)\r\n<\/pre>\n<p>Show tables in the current database:<\/p>\n<pre>astertest=> \\d\r\n                   List of relations\r\n Schema |          Name          |   Type   |   Owner   \r\n--------+------------------------+----------+-----------\r\n public | sip_conf_office        | table    | astertest\r\n public | sip_conf_office_id_seq | sequence | astertest\r\n(2 rows)<\/pre>\n<p>Show table structure:<\/p>\n<pre>astertest=> \\d sip_conf_office\r\n                                     Table \"public.sip_conf_office\"\r\n     Column     |          Type          |                          Modifiers                           \r\n----------------+------------------------+--------------------------------------------------------------\r\n id             | integer                | not null default nextval('sip_conf_office_id_seq'::regclass)\r\n name           | character varying(80)  | not null default ''::character varying\r\n accountcode    | character varying(20)  | \r\n amaflags       | character varying(7)   | \r\n callgroup      | character varying(10)  | \r\n callerid       | character varying(80)  | \r\n canreinvite    | character varying(3)   | default 'no'::character varying\r\n context        | character varying(80)  | \r\n defaultip      | character varying(15)  | \r\n dtmfmode       | character varying(7)   | \r\n fromuser       | character varying(80)  | \r\n fromdomain     | character varying(80)  | \r\n host           | character varying(31)  | not null default ''::character varying\r\n insecure       | character varying(4)   | \r\n language       | character varying(2)   | \r\n mailbox        | character varying(50)  | \r\n md5secret      | character varying(80)  | \r\n nat            | character varying(5)   | not null default 'no'::character varying\r\n...<\/pre>\n<p>To insert many lines in SQL-syntax from txt file:<\/p>\n<pre>\r\npsql -U asterisk -h localhost -d asterisk -f \/root\/peers.txt\r\n<\/pre>\n<p>&#8230; assuming that peers.txt looks like:<\/p>\n<pre>\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('0001','Operator 0001 <0001>','0001','operators','5060');\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('0001d','Operator 0001 <0001>','0001d','operators','5061');\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('0002','Operator 0002 <0002>','0002','operators','5060');\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('0002d','Operator 0002 <0002>','0002d','operators','5061');\r\n...\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('9999','Operator 9999 <9999>','9999','operators','5060');\r\nINSERT INTO sip_conf_new (name,callerid,defaultuser,context,port) VALUES ('9999d','Operator 9999 <9999>','9999d','operators','5061');\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Edit postgresql.conf line listen_addresses = &#8216;10.14.1.21&#8217; to your appropriate value. 2. Add to pg_hba.conf a line, which will allow connection to the database from certain IP address\/subnet: host johnsdb john 10.14.1.201\/32 md5 3. service postgresql reload 4. As root, &#8216;su &#8211; postgres&#8216; , then &#8216;psql&#8216; , then: 4.1. postgres=# CREATE USER john WITH ENCRYPTED [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[94,140,93],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-postgresql","tag-psql","tag-sql"],"_links":{"self":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=542"}],"version-history":[{"count":20,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":861,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions\/861"}],"wp:attachment":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}