postgress import data from a database to another one which has tables with more columns - postgresql

I have two postgres 9.6 databases on different machines:
db1 - older version with the data I need
db2 - the schema was updated and now it contains more columns. I only use the schema here. I don't need the data
I would like to bring all the data from db1 to db2 which has the newer schema. There are quite a few tables and I wouldn't want to alter them manually.
How would I achieve this?

You can use copy command with column name specified copy table_name (column...) to .... and copy table_name (column...) from ...
Copy to for export and copy from for import
https://www.postgresql.org/docs/10/sql-copy.html
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE { column_name [, ...] | * } ] ] ]
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | STDIN }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE NOT NULL column_name [, ...] ] ] ]

Related

MongoDB spatial query opposite of $geoIntersect, outside of polygon

Is it possible to perform a query where a point is outside a polygon?
I'm not really sure how to word my question, but I'm trying to do this with my query
I want to check if location A (Pickup) is in inside, and location B (dropoff) is outside
I thought of using $ne and $not but I get the error "unknown top level operator: $not"
$pickupPoint = [
'type' => 'Point',
'coordinates' => [
floatval($this->item['longitude']),
floatval($this->item['latitude'])
]
];
$dropPoint = [
'type' => 'Point',
'coordinates' => [
floatval($this->item['longitude']),
floatval($this->item['latitude'])
]
];
// Is in pickup, but outside dropoff
$pickupNotDropoff = [
'$and' => [
[
'Pickup' => [
'$geoIntersects' => [
'$geometry' => $pickupPoint,
]
]
],
[
'$not' => [
'Dropoff' => [
'$geoIntersects' => [
'$geometry' => $dropPoint,
]
]
]
]
]
];
Sample of data

mongoDB geospatial query doesn't works

I'm trying to extract all the elements inside a polygon (regular or irregular) but mongoDB geospatial doesn't works.
When I do the query like this, works perfectly
db.getCollection('houses').find({
'coordinates_geojson.coordinates.1': {
'$lte': 20.49584842128357,
'$gte': 20.458539491985142
},
'coordinates_geojson.coordinates.0': {
'$lte': -103.4088134765625,
'$gte': -103.47747802734375
}
})
but if try to query using geoWithin, doesn't return me anything
db.getCollection('houses').find({
"coordinates_geojson": {
"$geoWithin": {
"$geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-103.47747802734375,
20.458539491985142
],
[
-103.4088134765625,
20.458539491985142
],
[
-103.4088134765625,
20.49584842128357
],
[
-103.47747802734375,
20.49584842128357
],
[
-103.47747802734375,
20.458539491985142
]
]
]
]
}
}
}
})
Any help or idea is welcome
That's the correct answer, first unset the field coordinates_geojson.deviation_level and then build the index db.collection.createIndex({"coordinates_geojson" : "2dsphere" }).
Thank you #B.Fleming

aggregate function not working php codeigniter

I wrote a aggregate query to get data in mongodb. same query I need to run via php codigniter,
This is my mongodb query
db.states.aggregate([
{
$project: {
states: {
$filter: {
input: "$states",
as: "state",
cond: { $eq: [ "$$state.country_id", "101" ] }
}
}
}
}
])
same query i need to convert for php codignater. I have used this library to perform the db operarion.
I tried:
$pipeline = [
[
'$project' => [
'states' =>[
'$filter' => [
'input' => '$states',
'as' => 'state',
'cond' => [
'$eq' => [
'$$state.country_id' => $country_id
]
]
]
]
]
]
];
$result = $this->mongo_db2->aggregate('tb10_states', $pipeline);
But, its throw Message: Unrecognized expression '$$state.country_id'
Suggest me How to fix this.

How to make script increment value every 1.5 seconds when triggered

I have been working on adding a 1.5 second delay to the "inc %x_note_id" portion but instead of it just doing it with one message at a time until the last one, it delays and then sends them all at once. How do I make it where it delays each increment of %x_note_id in this porton of the script?
the full code is here
alias postmessage {
if ( $nick == $me ) { return }
var %x_note_id 1
while ( %noteidnick. [ $+ [ $server ] ] [ $+ [ . ] ] [ $+ [ $chan ] ] [ $+ [ . ] ] [ $+ [ $nick ] ] > 0 ) {
msg $chan %notemsg. [ $+ [ $server ] ] [ $+ [ . ] ] [ $+ [ $chan ] ] [ $+ [ . ] ] [ $+ [ $nick ] ] [ $+ [ . ] ] [ $+ [ %x_note_id ] ]
unset %notenick. [ $+ [ $server ] ] [ $+ [ . ] ] [ $+ [ $chan ] ] [ $+ [ . ] ] [ $+ [ $nick ] ] [ $+ [ . ] ] [ $+ [ %x_note_id ] ]
unset %notemsg. [ $+ [ $server ] ] [ $+ [ . ] ] [ $+ [ $chan ] ] [ $+ [ . ] ] [ $+ [ $nick ] ] [ $+ [ . ] ] [ $+ [ %x_note_id ] ]
var %note_delay 10000
while ( %note_delay ) {
dec %note_delay
if ( %note_delay == 0 ) {
inc %x_note_id
dec %noteidnick. [ $+ [ $server ] ] [ $+ [ . ] ] [ $+ [ $chan ] ] [ $+ [ . ] ] [ $+ [ $nick ] ]
}
}
}
}
Fixing your existing code, will require more work and will force me to work under your code restrictions.
I've decided to construct new message queuing, which will be more explicit and detailed. Which will result in easier time when you will need to extend it.
ON $*:TEXT:/^!note\s\w+\s/iS:#: {
if ($nick isin %twitchbots) return
; Bot command flood protection, 3 messages per 10 seconds.
inc -u10 % [ $+ [ $+(bpf.,$server,.,#,.,$wildsite) ]
var %bpf = % [ $+ [ $+(bpf.,$server,.,#,.,$wildsite) ]
if (%bpf >= 4) return
; Verify the sender doesn't reach his max quota
if ($userMessages($server, #, $nick).sent == 5) {
msg # $nick $+ , Limit reached. Only 5 messages per user is allowed.
return
}
saveMessage $server # $nick $2-
msg # $nick $+ , Your message has been left for $2 $+ .
}
ON !*:JOIN:#: playmessages $server # $nick
ON *:TEXT:*:#: playmessages $server # $nick
alias -l findServerConnectionIdx {
if (!$1) return
var %i = 1, %length = $scon(0)
while (%i <= %length) {
var %server = $scon(%i).server
if (%server == $1) {
return %i
}
inc %i
}
return
}
alias -l userMessages {
; $1 = server, $2 = channel, $3 = nick
if (!$3) return
if ($prop == sent) {
var %i = 1, %length = $var($+(notemsg.,$1,.,$2,.*), 0)
var %numOfSentMsgs = 0
while (%i <= %length) {
var %messages = $var($+(notemsg.,$1,.,$2,.*), %i).value
inc %numOfSentMsgs $wildtok(%messages, $3 *, 0, 9)
inc %i
}
return %numOfSentMsgs
}
else if ($prop == recv) {
var %messages = $var($+(notemsg.,$1,.,$2,.,$3), 1).value
return $numtok(%messages, 9)
}
}
alias -l saveMessage {
; $1 = server, $2 = channel, $3 = dest-nick, $4 = src-nick, $5- = message
if (!$5) return
set % [ $+ [ $+(notemsg.,$1,.,$2,.,$3) ] ] $addtok(% [ $+ [ $+(notemsg.,$1,.,$2,.,$3) ] ], $4-, 9)
}
alias -l clearRecvMessages {
; $1 = server, $2 = channel, $3 = nick
if (!$3) return
unset % [ $+ [ $+(notemsg., $1,.,$2,.,$3) ] ]
}
alias -l playMessages {
; $1 = server, $2 = channel, $3 = nick, $4 = delay
if (!$3) return
var %messages = % [ $+ [ $+(notemsg., $1,.,$2,.,$3) ] ]
var %i = 1, %length = $numtok(%messages, 9)
while (%i <= %length) {
var %fullMessage = $gettok(%messages, %i, 9)
var %sourceNick = $gettok(%fullMessage, 1, 32)
var %message = $gettok(%fullMessage, 2-, 32)
var %sconServer = $findServerConnectionIdx($1)
$+(.timernotes.,notemsg.,$1,.,$2,.,$3,.,%i) 1 $calc((%i - 1) * 1.5) scid -t1 %sconServer msg $2 From: %sourceNick Message: %message
inc %i
}
clearRecvMessages $1-3
}

MongoDB - $geoWithin query using $polygon or $geometry gives different result

Using MongoDB 3.2 i'm trying to use 2dsphere queries on a collection of Points.
Lets say i have à collection cust_5_abcd with a 2dsphere index on the_geom field.
Add a geometry in the collection :
db.cust_5_abcd.insert({
"chps0" : "Texte d'une ligne",
"the_geom" : {
"type" : "Point",
"coordinates" : [
1.032715,
40.380028
]
}})
Now i'm trying to query this Point using $geoWithin to get all data inside a specific Polygon. This is where i'm getting different result if i use $geometry with the GeoJSON definition , or with $polygon and the strict coordinates. Maybe something in the documentation is missing or something i misunderstood.
With $geometry gives NO result:
db.cust_5_abcd.find( { the_geom:
{ $geoWithin:
{ $geometry:
{
"type": "Polygon",
"coordinates": [
[
[ -16.237793, 40.162083 ],
[ -16.237793, 51.835778 ],
[ -13.776855, 51.835778 ],
[ -13.776855, 41.426253 ],
[ 14.765625, 41.426253 ],
[ 14.765625, 40.162083 ],
[ -16.237793, 40.162083 ]
]
]
}
}
}
})
With $polygon returns my Point :
db.cust_5_abcd.find( { the_geom:
{ $geoWithin:
{ $polygon:
[
[ -16.237793, 40.162083 ],
[ -16.237793, 51.835778 ],
[ -13.776855, 51.835778 ],
[ -13.776855, 41.426253 ],
[ 14.765625, 41.426253 ],
[ 14.765625, 40.162083 ],
[ -16.237793, 40.162083 ]
]
}
}
})
Got the answer from the mongodb people : https://jira.mongodb.org/browse/SERVER-24549?focusedCommentId=1293398&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1293398
When using $polygon a planar geometry is calculated, and the point falls inside the polygon you describe. But when you use GeoJSON a spherical geometry is used, and the point falls outside the geometry
Now the problem is that i cannot use $polygon on $geoIntersect for exemple or with other geometry then a polygon.