<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8816876163500939110</id><updated>2012-03-22T04:13:10.396-07:00</updated><category term='PostgreSQL'/><title type='text'>coherent light</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://2ndquadrant.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8816876163500939110/posts/default'/><link rel='alternate' type='text/html' href='http://2ndquadrant.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Harald Armin Massa</name><uri>http://www.blogger.com/profile/15991203403392171194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://2.bp.blogspot.com/-F5y3hGIxvjk/TXOOH5S5n7I/AAAAAAAAAAM/G4e60XdjUkQ/s220/Armin.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8816876163500939110.post-8713366977630463886</id><published>2011-03-08T08:32:00.000-08:00</published><updated>2011-04-07T10:46:30.851-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='PostgreSQL'/><title type='text'>quick hack to roughly transfer table structures from Oracle to PostgreSQL</title><content type='html'>Sometimes you just wish to mirror some data in PostgreSQL, which is stored by rules you cannot influence in Oracle. (For full-scale migration also check out &lt;a href="http://ora2pg.projects.postgresql.org/"&gt;ora2pg,&lt;/a&gt; and you can also order &lt;a href="http://www.2ndquadrant.com/postgresql-services/upgrades-and-migrations/#migrations"&gt;professional migration services&lt;/a&gt; :) )&lt;br /&gt;&lt;br /&gt;To transfer only a small number of table structures, I found some lines of Python really handy:&lt;br /&gt;&lt;pre&gt;&lt;a href="http://cx-oracle.sourceforge.net/"&gt;import cx_Oracle &lt;/a&gt;# I recommend using the unicode variant&lt;br /&gt;cn=cx_Oracle.connect(u"user",u"pwd",u"tnsname")&lt;br /&gt;cs=cn.cursor()&lt;br /&gt;# just select one row from the table we want to mirror&lt;br /&gt;# bonus point: with naming the columns we also can mirror only part of that table&lt;br /&gt;cs.execute("select * from tabletomirror where rownum &amp;lt; 2")&lt;br /&gt;&lt;br /&gt;# this defines a mapping between cx_Oracle types and PostgreSQL types.&lt;br /&gt;# as the title states, we are talking about "roghly"&lt;br /&gt;# improving the "numeric" by using precision, scale etc. is left as an exercise for the reader&lt;br /&gt;# (as is mapping CLOBS and whatever else there might be .. :) )&lt;br /&gt;ora2pg= {cx_Oracle.NUMBER:"numeric", cx_Oracle.UNICODE:"text", &lt;br /&gt;         cx_Oracle.STRING:"text",cx_Oracle.DATETIME:"timestamp"}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# cs.description contains a good-enough description of the oracle&lt;br /&gt;# table structure&lt;br /&gt;# YES, primary keys etc. are not transferred automatically ...&lt;br /&gt;# that's why I call it a quick hack &lt;br /&gt;collist=[]&lt;br /&gt;for oname, otyp, display_size, internal_size, \&lt;br /&gt;   precision, scale, null_ok in cs.description:&lt;br /&gt;    collist.append("{oname} {otype}".format(&lt;br /&gt;                        oname=oname, otype=ora2pg[otyp]))&lt;br /&gt;&lt;br /&gt;createtablecommand = """create table tabnameinpgsql as ({0});""".format(&lt;br /&gt;                     ", ".join(collist))&lt;br /&gt;print ctablecommand&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8816876163500939110-8713366977630463886?l=2ndquadrant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://2ndquadrant.blogspot.com/feeds/8713366977630463886/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://2ndquadrant.blogspot.com/2011/03/quick-hack-to-roughly-transfer-table.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8816876163500939110/posts/default/8713366977630463886'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8816876163500939110/posts/default/8713366977630463886'/><link rel='alternate' type='text/html' href='http://2ndquadrant.blogspot.com/2011/03/quick-hack-to-roughly-transfer-table.html' title='quick hack to roughly transfer table structures from Oracle to PostgreSQL'/><author><name>Harald Armin Massa</name><uri>http://www.blogger.com/profile/15991203403392171194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://2.bp.blogspot.com/-F5y3hGIxvjk/TXOOH5S5n7I/AAAAAAAAAAM/G4e60XdjUkQ/s220/Armin.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8816876163500939110.post-8511705624557537110</id><published>2011-03-06T06:06:00.000-08:00</published><updated>2011-04-07T10:46:30.851-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='PostgreSQL'/><title type='text'>switchover for the common people</title><content type='html'>Streaming Replication in PostgreSQL 9.0 made it a joy to install a hot standby server. It gets easier if you use the excellent 2ndQuadrant clusterware &lt;a href="http://projects.2ndquadrant.com/repmgr"&gt;repmgr&lt;/a&gt;. The main challenge is coming up with&amp;nbsp;a proper naming schema which avoids using „master“ or „standby“, as all participants of replication are just playing roles, which may be switched; and&amp;nbsp;setting up proper authorizations, ssh-keys and all.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Still there is one area, where many of the documentation point it out as an excercise for the reader: when doing the switchover, so the former standby becomes the new master – how do we inform all database users?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;One solution is to have&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: cyan;"&gt;&lt;a href="http://pgfoundry.org/projects/pgbouncer/"&gt;PgBouncer &lt;/a&gt;or &lt;a href="http://pgpool.projects.postgresql.org/"&gt;PgPool &lt;/a&gt;running on a third server&lt;/span&gt; (as „running on one of the database servers“ will take PgBouncer / PgPool down when that server goes down), and have clients connect to the database via pgbouncer / pgpool&amp;nbsp;&lt;/div&gt;&lt;div&gt;The nice things:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;all the additional powers of those tools – i.e. pooling connections: read the linked websites for details and more powers&lt;/li&gt;&lt;li&gt;no heckling with network infrastructure (IP, DNS)&lt;/li&gt;&lt;li&gt;at least pgbouncer is also available on windows&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;The not-so-nice things:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;neading another server (that also should be kept redundand, giving an additional level to the switchover-challenge as in „switching over to the backup pgbouncer/pgpool)&lt;/li&gt;&lt;li&gt;having another tool to configure and maintain&lt;/li&gt;&lt;li&gt;authorization and encryption become more challenging through pgbouncer/pgpool being an involved third party&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;div&gt;The second idea is to deal with the switchover on IP level: the &lt;span class="Apple-style-span" style="background-color: cyan;"&gt;new master can take over the IP-address&lt;/span&gt; of the old master&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Nice about this: Clients can connect to the old IP-address&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Challenges:&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;make sure the old master is really taken of the network or switched to a third IP-address (so no quick testing with "pg_ctl stop" on the master)&lt;/li&gt;&lt;li&gt;is the DBA allowed to change IP addresses (network infrastructure may be handled by a totally different department / company)&lt;/li&gt;&lt;li&gt;how quick and reliable are the updates of the IP-MAC-caches in switches / routers, especially in WAN environments&lt;/li&gt;&lt;li&gt;hassle with database-monitoring software which checks on both roles (and be it simply PgAdmin refreshing the "SELECT * FROM repl_status")&lt;/li&gt;&lt;li&gt;are the used backup solutions provable happy with that kind of IP-changes?&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;Third option is doing the switchover using the dns-server: have a &lt;span class="Apple-style-span" style="background-color: cyan;"&gt;DNS-name-alias „actualmasterdb“&lt;/span&gt; for the active server, have that alias point to the actual master server – hence the name :)&lt;/div&gt;&lt;div&gt;&lt;div&gt;Challenges:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;is the DBA qualified to reconfigure the DNS-server ( DNS-server may be running on a different operating system / or be an appliance)&lt;/li&gt;&lt;li&gt;how to script the reconfiguration of the DNS-Server&amp;nbsp;&lt;/li&gt;&lt;li&gt;is the DBA allowed to reconfigure the DNS-server (network infrastructure in other department / company)&lt;/li&gt;&lt;li&gt;how to update all DNS-caches accross the network (especially on the clients, intensly looking at you, windows DNS name cache)&lt;/li&gt;&lt;li&gt;can database monitoring cope with the changes?&amp;nbsp;&lt;/li&gt;&lt;li&gt;double check that backup knows whom it is backuping&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;The three presented solutions make you pay their – and especially those „network infrastructure in different deparment“ may be a prohibitive situation.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: lime;"&gt;libpq to the rescue&lt;/span&gt;: since &lt;a href="http://git.postgresql.org/gitweb?p=postgresql.git;a=commitdiff;h=f2ef4df4a39e7d5c539dd51a752628e59b656f04;hp=1333f07ff19707353f7c2cc21f6aa9124f5b1b16"&gt;2000-10-17&lt;/a&gt;, the option of a pg_service.conf file is within libpq. &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To use it, use the following receipt:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;create a&lt;a href="http://www.postgresql.org/docs/current/static/libpq-pgservice.html"&gt; pg_service.conf file&lt;/a&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;# comment: ncc1701 is master at stardate 2011-03-06&amp;nbsp;&lt;/span&gt;&lt;br&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;[myactivedatabase]&lt;/span&gt;&lt;br&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;host=ncc1701&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;port=5432&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;database=criticalmission&lt;/span&gt;&lt;/li&gt;&lt;li&gt;put that pg_service.conf on a network file share&amp;nbsp;&lt;/li&gt;&lt;li&gt;point the environment variable PGSYSCONFDIR &amp;nbsp;to that directory (on windows &lt;a href="http://www.codeproject.com/KB/applications/SetEnv.aspx"&gt;setenv&lt;/a&gt; may come in handy), alternatively point PGSERVICEFILE to the pg_service.conf&amp;nbsp;&lt;/li&gt;&lt;li&gt;connect to your active database via a connection parameter of „service=myactivedatabase“&amp;nbsp;in any libpq application&lt;/li&gt;&lt;li&gt;on switchover, simply change the pg_service.conf file so that „host“ points to the promoted standby. Have the clients build up a new connection (as the old connection will be broke anyway, since the old master is gone)&lt;/li&gt;&lt;li&gt;done.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0cm;"&gt;Challenges of this solution:&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;div style="margin-bottom: 0cm;"&gt;not every libpq-application  supports entering service=servicename for its connection parameters  (that's a limit of those applcations connection handlers, not of libpq!); allthough,  there is allways hope that expand_dbname is non-zero and you can  enter „service=servicename“ for the database-name and libpq will  &lt;a href="http://www.postgresql.org/docs/9.0/static/libpq-connect.html"&gt;parse it correctly&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div style="margin-bottom: 0cm;"&gt;solutions have to be found what to  do if the file-share goes down (have a local copy of the  pg_service.conf, how to update that ...) But in practice most  enterprises come to a halt anyway when &lt;a href="http://norvig.com/Gettysburg/sld001.htm"&gt;strategic .ppt-files&lt;/a&gt; - also  on fileshares - are no longer available; so somebody else has  allready taken care of the fileshare high availability.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-bottom: 0cm;"&gt;disclaimer: I am the&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: arial,helvetica,sans-serif; font-size: 13px; line-height: 18px;"&gt;&lt;a href="http://www.dict.cc/englisch-deutsch/owner+manager.html" style="color: black; text-decoration: none;"&gt;owner-manager&lt;/a&gt;&amp;nbsp;&lt;/span&gt;of 2ndQuadrant Deutschland GmbH, the German branch of the international professional &lt;a href="http://www.2ndquadrant.com/"&gt;PostgreSQL service company 2ndQuadrant&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8816876163500939110-8511705624557537110?l=2ndquadrant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://2ndquadrant.blogspot.com/feeds/8511705624557537110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://2ndquadrant.blogspot.com/2011/03/switchover-for-common-people.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8816876163500939110/posts/default/8511705624557537110'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8816876163500939110/posts/default/8511705624557537110'/><link rel='alternate' type='text/html' href='http://2ndquadrant.blogspot.com/2011/03/switchover-for-common-people.html' title='switchover for the common people'/><author><name>Harald Armin Massa</name><uri>http://www.blogger.com/profile/15991203403392171194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://2.bp.blogspot.com/-F5y3hGIxvjk/TXOOH5S5n7I/AAAAAAAAAAM/G4e60XdjUkQ/s220/Armin.jpg'/></author><thr:total>2</thr:total></entry></feed>
