<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://rt-wiki.bestpractical.com/index.php?action=history&amp;feed=atom&amp;title=SQLite2PG</id>
	<title>SQLite2PG - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://rt-wiki.bestpractical.com/index.php?action=history&amp;feed=atom&amp;title=SQLite2PG"/>
	<link rel="alternate" type="text/html" href="https://rt-wiki.bestpractical.com/index.php?title=SQLite2PG&amp;action=history"/>
	<updated>2026-08-21T21:54:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://rt-wiki.bestpractical.com/index.php?title=SQLite2PG&amp;diff=3332&amp;oldid=prev</id>
		<title>Admin: 2 revisions imported</title>
		<link rel="alternate" type="text/html" href="https://rt-wiki.bestpractical.com/index.php?title=SQLite2PG&amp;diff=3332&amp;oldid=prev"/>
		<updated>2016-04-06T20:36:22Z</updated>

		<summary type="html">&lt;p&gt;2 revisions imported&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The code below uses a simple file copy of the [[SQLite]] database and inserts it into a [[PostgreSQL]] database that has been prepared with &amp;lt;code&amp;gt; make initialize-database&amp;lt;/code&amp;gt;. It&amp;#039;s probably a bit rough but worked reasonably for me. I&amp;#039;d appreciate hearing about any comments, successes or failures you have with this.&lt;br /&gt;
&lt;br /&gt;
Note that the sequences need to be renormalized after this completes for everything to work correctly. I&amp;#039;ve also pasted my commandline to do that below the script.&lt;br /&gt;
&lt;br /&gt;
This code is posted in the hopes that it will be usefull but without any guarantee or warranty. I place it in the public domain.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#!/usr/bin/perl&lt;br /&gt;
 &lt;br /&gt;
 use strict;&lt;br /&gt;
 use warnings;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 use DBIx::ContextualFetch;&lt;br /&gt;
 &lt;br /&gt;
 my $select = &amp;quot;select * from %s&amp;quot;;&lt;br /&gt;
 my $insert = &amp;quot;insert into %s ( %s ) values ( %s )&amp;quot;;&lt;br /&gt;
 my $delete = &amp;quot;delete from %s&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 my $sqlite = DBI-&amp;amp;gt;connect(&lt;br /&gt;
     &amp;#039;dbi:SQLite:dbname=./rt3.db&amp;#039;,&lt;br /&gt;
     &amp;#039;&amp;#039;, &amp;#039;&amp;#039;,&lt;br /&gt;
     {&lt;br /&gt;
         AutoCommit =&amp;amp;gt; 0,&lt;br /&gt;
         RaiseError =&amp;amp;gt; 1,&lt;br /&gt;
        RootClass  =&amp;amp;gt; &amp;quot;DBIx::ContextualFetch&amp;quot;,&lt;br /&gt;
     }&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 my $pg = DBI-&amp;amp;gt;connect(&lt;br /&gt;
     &amp;#039;dbi:Pg:dbname=rt3&amp;#039;,&lt;br /&gt;
     &amp;#039;rt&amp;#039;, &amp;#039;&amp;#039;,&lt;br /&gt;
    { AutoCommit =&amp;amp;gt; 0, RaiseError =&amp;amp;gt; 1 }&lt;br /&gt;
 );&lt;br /&gt;
 &lt;br /&gt;
 for my $table ($sqlite-&amp;amp;gt;tables(&amp;#039;%&amp;#039;, &amp;#039;%&amp;#039;, &amp;#039;%&amp;#039;) ) {&lt;br /&gt;
 &lt;br /&gt;
     my %default = (&lt;br /&gt;
         integer =&amp;amp;gt; 0,&lt;br /&gt;
         &amp;#039;timestamp without time zone&amp;#039; =&amp;amp;gt; &amp;#039;1970-01-01 00:00:00&amp;#039;,&lt;br /&gt;
     );&lt;br /&gt;
 &lt;br /&gt;
     my %type = ();&lt;br /&gt;
 &lt;br /&gt;
     $table =~ s/&amp;quot;//g;&lt;br /&gt;
     print &amp;quot;------- table $table \n&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
     # skip sqlite meta-tables&lt;br /&gt;
     next if ($table =~ /^sqlite_/);&lt;br /&gt;
 &lt;br /&gt;
     # get the the source rows&lt;br /&gt;
     my $sel = $sqlite-&amp;amp;gt;prepare( sprintf($select, $table ) );&lt;br /&gt;
     $sel-&amp;amp;gt;execute();&lt;br /&gt;
     my @row = $sel-&amp;amp;gt;fetchall_hash;&lt;br /&gt;
 &lt;br /&gt;
     # remove all the old data&lt;br /&gt;
     $pg-&amp;amp;gt;do( sprintf( $delete, $table ) );&lt;br /&gt;
 &lt;br /&gt;
     for my $r (@row) {&lt;br /&gt;
 &lt;br /&gt;
         # give the database a chance to use its defaults&lt;br /&gt;
         for my $c (keys %{$r} ){&lt;br /&gt;
             if (not defined $r-&amp;amp;gt;{$c} or $r-&amp;amp;gt;{$c} eq &amp;#039;&amp;#039;) {&lt;br /&gt;
                 delete $r-&amp;amp;gt;{$c};&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
 &lt;br /&gt;
         # figure out the insert statement&lt;br /&gt;
         my $q = sprintf( $insert,&lt;br /&gt;
             $table,&lt;br /&gt;
             join( &amp;#039;,&amp;#039;, keys %{$r} ),&lt;br /&gt;
             join( &amp;#039;,&amp;#039;, (&amp;#039;?&amp;#039;) x scalar keys %{$r} ),&lt;br /&gt;
         );&lt;br /&gt;
         my $ins = $pg-&amp;amp;gt;prepare( $q );&lt;br /&gt;
 &lt;br /&gt;
         $ins-&amp;amp;gt;execute( values %{$r} );&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $sqlite-&amp;amp;gt;disconnect();&lt;br /&gt;
 &lt;br /&gt;
 $pg-&amp;amp;gt;commit();&lt;br /&gt;
 $pg-&amp;amp;gt;disconnect();&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command line for resetting sequences.&lt;br /&gt;
&lt;br /&gt;
 sudo perl -lane &amp;#039;&lt;br /&gt;
         /CREATE SEQUENCE (\S[^_]+)(\S+);/ or next;&lt;br /&gt;
         print qq|select setval(\047$1$2\047, (select max(id) from $1)+1);|;&lt;br /&gt;
 &amp;#039; schema.Pg  | psql -Upostgres rt3&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>