postgresql restoring from dumpall file. Indexes and Constraints not being created - database

I'm upgrading from and old psql version to a newer (9.5 to 15 (for Ubuntu 20.04)) and when i'm trying to restore from dumpfile everything seems fine except that indexes are not being created.
I made the dumpfile by doing
pg_dumpall > dbdump.dump
Then, after upgrading cluster and everything i'm restoring the DB by doing
psql -f dbdump.dump postgres as suggested by PostgreSQL Documentation.
When i check indexes with select * from pg_indexes where tablename not like 'pg%'; there's nothing.
There should be at least 10K+ of indexes (yes, the DB has more than 10K tables, so creating the indexes by hand is not an option)
What could be wrong?

In this case you should check the PostgreSQL log file. If the restore fails, there must be an error in the log file, or even in the console. There are a few more details that would help clarify the situation. For example, are you using a GUI tool for restoring? Or are you running the command from the CLI?

Related

Restore corrupt mongo db from WiredTiger files

So here is my scenario:
Today my server was restarted by our hoster (acpi shutdown).
My mongo database is a simple docker container (mongo:3.2.18)
Because of an unknown reason the container wasn't restarted on reboot (restart: always was set in docker-compose).
I started it and noticed the volume mapping were gone.
I restored them to the old paths, restarted the mongo container and it started without errors.
I connected to the database and it was completely empty.
> show dbs
local 0.000GB
> use wekan
switched to db wekan
> show collections
> db.users.find();
>
Also I already tried db.repairDatabase();, no effect.
Now my _data directory contains a lot of *.wt files and more. (File list)
I found collection-0-2713973085537274806.wt which has a file size about 390MiB.
This could be the data I need to restore, assuming its size.
Any way of restoring this data?
I already tried my luck using wt salvage according to this article, but I can't get it running - still trying.
I know backups,backups,backups! Sadly this database wasn't backuped.
Related GitHub issue, contains details to software.
Update:
I was able to create a .dump file with the WiredTiger Data Engine tool. However I can't get it imported into a mongoDB.
Try running a repair on the mongo db container. It should repair your database and the data should be completely restored.
Start mongo container in bash mode.
sudo docker-compose -f docker-compose.yml run mongo bash
or
docker run -it mongo bash
Once you are inside the docker container, run mongo db repair.
mongod --dbpath /data/db --repair
The DB should repaired successfully and all your data should be restored.

Synchronize database files with the database - PostgreSQL

First forgive me for my English. It is a little bad. Second forgive my ignorance, i'm newiest in postgres
I'm having trouble when I try to up a backup database on another database. I need to dump the database just to get one table, but I only have the files that was in /var/lib/pgsql/data/base/
Here what I try:
I create a database named "test" with OID 227763 so I put the files of the old database to this new database with another OID. I fix the folder and files permissions, but when I log into "test" and run select * from pg_tables; the tables does not appears to me. And when I try to create the table on PhpPgAdmin, I got
ERROR: relation already exists
I'm trying to do this because I need to know which of this files is the table that i want. I will log into database and run SELECT oid,* from pg_class; to get the OID.
I found the old OID database in /var/lib/pgsql/data/global/pg_database
If anyone can help me, I thank you.
There are many ways to backup and restore an entire database or a single table. It sounds like you need to be using pgDump instead of working on individual files. A file level copy is likely to corrupt your database if not in backup mode and if not copying the entire thing + archive logs.
If you MUST copy it by files, make sure the database is shut down for maximum safety.
For me, if I had one table to backup, I'd use pg_dump
pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}
you can use the -t flag to list a single table if you like.

Dropping a postgres database in cmdline, still seeing the database when \list

I'm trying to drop my database and create a new one through the command line.
I login using psql postgres and then do a \list, see a list of the two databases i created which i now want to delete. so i tried using a DROP DATABASE databasename;
I don't see any error while executing that statement but when i try to \list again to see if that DB are deleted, i still see that that the DB exists. Can someone please tell me why this could happen? and how to surely delete those DB.
There are a couple caveats to DROP DATABASE:
It can only be executed by the database owner.
It cannot be executed while you or anyone else are connected to the target database.
I generally use the dropdb command-line tool to do this, since it's a wrapper around DROP DATABASE which doesn't require you to explicitly connect first. It still has the caveat that there can't be any users currently connected to the database, but it's generally quicker/easier to use.
I would recommend you try issuing a command like this:
dropdb -h <host> -U <user> -p <port> <name of db to drop>
Similarly, you can use the createdb command-line tool to create a database.
More info on DROP DATABASE: http://www.postgresql.org/docs/current/static/sql-dropdatabase.html
Edit:
Also, it is worth looking in the Postgres log (likely in /var/log/postgresql by default) to see if perhaps there is anything in there that wasn't surfaced in the results.

How to reset / clear / delete neo4j database?

We can delete all nodes and relationships by following query.
MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r
But newly created node get internal id as ({last node internal id} + 1) . It doesn't reset to zero.
How can we reset neo4j database such as newly created node will get id as 0?
From 2.3, we can delete all nodes with relationships,
MATCH (n)
DETACH DELETE n
Shut down your Neo4j server, do a rm -rf data/graph.db and start up the server again. This procedure completely wipes your data, so handle with care.
run both commands.
match (a) -[r] -> () delete a, r
above command will delete all nodes with relationships.
then run ,
match (a) delete a
and it will delete nodes that have no relationships.
Dealing with multiple databases.
According to Neo4j manage multiple databases documentation:
One final administrative difference is how to completely clean out one database without impacting the entire instance with multiple databases. When dealing with a single instance and single database approach, users can delete the entire instance and start fresh. However, with multiple databases, we cannot do that unless we are comfortable losing everything from our other databases in that instance.
The approach is similar to other DBMSs where we can drop and recreate the database, but retain everything else. Cypher’s command for this is CREATE OR REPLACE DATABASE <name>. This will create the database (if it does not already exist) or replace an existing database with a clean one.
When neo4j is initiated, it is possible to access two databases, a system database and a default (neo4j) database. To clear/reset neo4j database:
1 - Switch to system database:
:use system
2 - Show all databases created with the instance:
SHOW DATABASES
3 - Run the command to clear the database.
CREATE OR REPLACE DATABASE <name>
In my experience, there are two ways to reset a Neo4j database, depending on what you need.
Method 1: Simply delete all nodes/relationships/indexes/constraints
In Neo4j Browser, or in Py2neo with graph.run() (link).
# All nodes and relationships.
MATCH (n) DETACH DELETE n
# All indexes and constraints.
CALL apoc.schema.assert({},{},true) YIELD label, key RETURN *
However, despite being convenient, this approach is not suitable in case of using command neo4j-admin.bat import for BULK import, i.e. ideal for importing millions of nodes at once quickly.
Method 2: Reset database for BULK Import Tool
It's not possible to BULK import when the database is not empty. I tried the above method, but still received the error:
Import error: C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here
Caused by:C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here
java.lang.IllegalStateException: C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here
To tackle this issue, I deleted the following folders:
c:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j
and
c:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\transactions\neo4j
Then carried out the Import command:
"C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\bin\neo4j-admin.bat" import --database=neo4j --multiline-fields=true --nodes=node_ABC.csv --nodes=node_XYZ.csv relationships=relationship_LMN.csv --relationships=relationship_UIO.csv
Start the Neo4j database. In Neo4j Desktop, the labels and relationships should now be recognized.
Notice that the database I deleted (neo4j) and the database I imported to are the same.
This worked for me with ver. 4.3.2 of the community editition:
Stop the server
cd <neo home>
rm -Rf data/databases/* data/transactions/*
Restart the server
Now you've again the system and the neo4j DBs. The command above deletes the system DB too, and that seems necessary, since deleting a regular DB only (which, in the community edition can only be 'neo4j') makes the metadata in the system DB inconsistent and you start seeing errors.
data/dbms seems to contain the user credentials and you can keep it if you want to keep existing users (else, you'll go back to the default neo4j/test user).
The recommended method is to use the DROP or CREATE Cypher commands, however, these are available in the enterprise edition only (I think it's a shame that a basic feature like this is part of their premium offer, but that's it).
This command deletes everything but requires apoc to be installed :
CALL apoc.periodic.iterate('MATCH (n) RETURN n', 'DETACH DELETE n', {batchSize:1000})
If you are using it on a docker container, you can do
docker-compose rm -f -s -v myNeo4jService
Since neo4j only runs current database specified in the conf file, an easy way to start a new and clean db is to change the current database in the neo4j.conf file and then restart neo4j server.
dbms.active_database=graph.db --> dbms.active_database=graph2.db
Some might argue that the database name is changed. But as of this writing [2018-12], neo4j doesn't support multiple database instances. There is no need for us to differentiate between databases, thus the name of the database is not used in our code.
You can clear/truncate the database with the command below:
MATCH (n) DETACH DELETE n
What this command does is, it matches all the nodes in the database, then detaches all the relationships the matched nodes have and finally deleting the nodes themselves.

Django sqlite - how to change schema

I have made changes to my model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:
syncdb: only works first time, not with changes
"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).
Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.
Suggestions?
First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do
sudo apt-get install sqlite3
On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.
Use it like this:
> sqlite3 /path/to/your/database
;show some help
.help
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;
See also the command line reference: http://www.sqlite.org/sqlite.html
and the sqlite SQL reference: http://www.sqlite.org/lang.html.
Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema in sqlite3, and the output of manage.py sql my_site to find out which columns you need to add.
An example:
ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text
Use Django's built in database management tool:
python manage.py dbshell
And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).

Resources