{"id":132,"date":"2005-08-12T12:31:05","date_gmt":"2005-08-12T12:31:05","guid":{"rendered":"http:\/\/www.bartbusschots.ie\/blog\/?p=132"},"modified":"2014-08-04T14:59:53","modified_gmt":"2014-08-04T14:59:53","slug":"switching-from-mysql-to-postgresql","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2005\/08\/12\/switching-from-mysql-to-postgresql\/","title":{"rendered":"Switching from MySQL to PostgreSQL"},"content":{"rendered":"<p><\/p>\n<p>I was recently forced to switch from MySQL to PostgreSQL for a project I am working on and at first I was very reluctant to change because I had been using MySQL for years, was very familiar with it, and knew my way round the developers section of the MySQL website. However, having worked with PSQL I would now never go back to MySQL, it is simply in a lower league than PSQL!<\/p>\n<p>When using a DB the differences are not all that big but when creating and admining DBs there are some differences that will throw you at first so I&#8217;ll list the ones I came across and how to get by them.<\/p>\n<h1>User Management<\/h1>\n<p>MySQL has a bizarre model of user management, the user <font face=\"courier new,courier,monospace\">bart<\/font> from say <font face=\"courier new,courier,monospace\">localhost<\/font> is considered a completely separate user to the user <font face=\"courier new,courier,monospace\">bart<\/font> from any host or the user <font face=\"courier new,courier,monospace\">bart<\/font> from a particular host. This makes managing users interesting and TBH I have always hated this aspect of MySQL. PSQL takes a simpler view, it uses the system accounts as its accounts. The user <font face=\"courier new,courier,monospace\">bart<\/font> is the user <font face=\"courier new,courier,monospace\">bart<\/font> no matter where he is logging in from and his password is his system password. Also, there is no root account as such on PSQL, if you access PSQL as the user who owns PSQL then you have &#8216;root&#8217; powers, if you don&#8217;t you only have the powers that have been given to you by the DBA with <font face=\"courier new,courier,monospace\">GRANT<\/font> statements.<\/p>\n<p>Control of access to different databases by different users from different places is all controlled from a text file called <font face=\"courier new,courier,monospace\">pg_hba.conf<\/font> in which you specify how PSQL should authenticate different users trying to access different DBs from different places. You have a number of choices ranging from &#8216;trust&#8217; which lets any system user access the DB as any user without a password to &#8216;password&#8217; which requires the user to supply a password and &#8216;ident&#8217; which only allows the user to log into the DB as the user they are logged into the system as. <font face=\"courier new,courier,monospace\">pg_hba.conf<\/font> is well commented with lots of examples so it is really easy to manage client authentication on PSQL and makes MySQL look over complicated and under-powered.<\/p>\n<p \/>\n<h1>AUTO_INCREMENT<\/h1>\n<p>There is no <font face=\"courier new,courier,monospace\">AUTO_INCREMENT<\/font> keyword in PSQL. This scared the crap out of me at first because I thought I had a real problem, but of course I didn&#8217;t, PSQL has a special data type for fields that need to auto increment called <font face=\"courier new,courier,monospace\">SERIAL<\/font>. This creates an implicit sequence which adds a slight complication in that you need to grant users access to BOTH the table AND the sequence if you want them to be able to add rows to the table. The sequence always has a name with the format: <font face=\"courier new,courier,monospace\">&amp;lt;table&amp;gt;_&amp;lt;field&amp;gt;_seq<\/font>.<\/p>\n<p \/>\n<h1>ENUM<\/h1>\n<p>Again PSQL does not have an <font face=\"courier new,courier,monospace\">ENUM<\/font> type which, at first, seems like a problem but you can create fields that are effectively enumerations using the <font face=\"courier new,courier,monospace\">CHECK<\/font> keyword (and you can do MUCH MUCH more with it too!). To set up an enumeration called <font face=\"courier new,courier,monospace\">user_level<\/font> with the possible values &#8216;STUDENT&#8217;, &#8216;MENTOR&#8217; and &#8216;ADMIN&#8217; you would use the following syntax:<\/p>\n<p><font face=\"courier new,courier,monospace\">user_level VARCHAR(10) DEFAULT &#8216;STUDENT&#8217; NOT NULL CHECK (user_level IN (&#8216;STUDENT&#8217;, &#8216;MENTOR&#8217;, &#8216;ADMIN&#8217;))<\/font><\/p>\n<p \/>\n<h1>Transactions<\/h1>\n<p>PSQL has excellent transaction support and it is stable and works with things like JDBC which is more than can be said for MySQL ATM. And, to make it even better, the syntax is immensely simple!<\/p>\n<pre>BEGIN;<\/pre>\n<pre>-- do work<\/pre>\n<pre>COMMIT;<\/pre>\n<h1>Shell<\/h1>\n<p>The <font face=\"courier new,courier,monospace\">psql<\/font> shell to access PSQL is very very different to the <font face=\"courier new,courier,monospace\">mysql<\/font> shell you access MySQL through. Basically you are just gonna have to RTFM it! Also, when you get stuck (and you will) just remember that <font face=\"courier new,courier,monospace\">\\h<\/font> gives you help and <font face=\"courier new,courier,monospace\">\\q<\/font> exits! The thing I missed most were MySQLs simple keywords <font face=\"courier new,courier,monospace\">SHOW<\/font> and <font face=\"courier new,courier,monospace\">DESCRIBE<\/font>, PSQL has the same functionality but just with much less memorable commands, e.g. <font face=\"courier new,courier,monospace\">\\dt<\/font> lists all the tables in the current DB.<\/p>\n<hr width=\"100%\" size=\"2\" \/>\n<p \/>\n<p \/>\n<p>Overall I find that PostgreSQL is a more powerful and more mature and robust piece of software than MySQL and since it is also open source I can see no reason to choose MySQL over PostgreSQL. Yes, if you have been using MySQL for years you will have a bit of a learning curve but trust me, it is well worth the bit of time and effort to do that learning!<\/p>\n<p \/>\n","protected":false},"excerpt":{"rendered":"<p>I was recently forced to switch from MySQL to PostgreSQL for a project I am working on and at first I was very reluctant to change because I had been using MySQL for years, was very familiar with it, and knew my way round the developers section of the MySQL website. However, having worked with [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[3,425,424],"series":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-databases","tag-mysql","tag-postgresql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-28","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":1,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":7412,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/132\/revisions\/7412"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=132"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}