I am very new to Postgres and the main problem I have now is that I don't have an overview of my server. If you use something like pgadmin3 it's easy to browse and to get a general idea of the structure of the database.
So looking for some general commands that could help me discover my database server.
Very basic commands:
Connect to database with client console
psql dbname
Dump db tables
\d
Dump a table schema
\d table
List table content
select * from table
General help
\?
Source/Further info: https://www.postgresql.org/docs/current/static/app-psql.html
Related
I am new to Postgresql and so far I have not found a way to drop a table from specific database. To give some context:
We are doing a synchronization from Oracle to PostgreSQL of 5 tables. In postgres I have a database SoloCopy and the schema is the default public. In the Postgresql instance we have also 2 more databases SoloSynch and postgres (the default one).
What I want to do is to select SoloCopy database and:
DROP TABLE public.table1;
When I do the above DROP statement table1 is deleted only from the database that was selected when opening SQL Query. But I want to specify the database before that and to be irrelevant from where the SQL Query was open. How can I do that?
I found an answer on my own. Setup can be found here:
Psql in Task Scheduler does not run a file script
Basically I needed to use PSQL and with the connection string there I connect to a specific DB, which I can drop the tables from. The details for PGPASSWORD and the .bat file I ended up creating are in the link above. Had to start everything from CMD and switch to psql from there.
I have a AWS RDS PostgreSQL 13 server with some databases. I have to create an empty copy of one database (empty means schema (tables, views, functions) + security (users, roles)).
Is pg_dump -s what I am looking for?
Thanks!
pg_dump -d db_name -s. You will also need to do pg_dumpall -g to get the global data e.g. roles. This will get all global data for the Postgres cluster, so you may have more then you need for the particular database.
Postgres allows the use of any existing database on the server as a template when creating a new database. I'm not sure whether pgAdmin gives you the option on the create database dialog but you should be able to execute the following in a query window if it doesn't:
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
Still, you may get:
ERROR: source database "originaldb" is being accessed by other users
To disconnect all other users from the database, you can use this query:
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();
Good afternoon.
Can you help me, I would like to know, how the longDescription table in IBMDB2 works and how do I bring the result which is inside the columns?
Thank you.
I believe that the longDescription is one of Maximo Asset Management tables.
If so, here is an example, how to handle it in Db2 on AIX/Linux.
logon as the instance owner on the machine, such as db2inst1
Run below at command prompt to list up all databases name(s) under the instance:
$ db2 list db directory
Then run below to use one of database name(s):
$ db2 connect to DBNAME
please replace DBNAME as your target database name, such as sample.
ie. db2 connect to sample
Then run below to list up all table names in the database DBNAME
$ db2 list tables
it will list all table names and its schema name, type and creation time
Then run below to list up all columns in the table.
$ db2 describe table SCHEMA.TABLENAME
replace SCHEMA like db2inst1 and TABLENAME like longDescription
ie. db2 describe table db2inst1.longDescription
it will list up all column names and some other information
Then run below to get data from a column, such as idownertable.
$ db2 select COLOMN_NAME from SCHEMA.TABLENAME
replace COLOMN_NAME like ldownertable
ie. db2 select ldownertable from db2inst1.longDescription
If we want to see all data in the table, run below:
$ db2 select * from SCHEMA.TABLENAME
ie. db2 select * from db2inst1.longDescription
Hope this helps.
I'm new to PostgreSQL, how can I select an existing database? I have successfully created a new database with this SQL command:
CREATE DATABASE testdb
But there is no such syntax as USE dbname like in MySql. I've already looked for solution on internet and I've found that the query for this purposes is \connect dbname.
But DataGrip tells 'Nothing to run' when I'm trying to execute that. What am I doing wrong? What is the correct way to select psql DB in DataGrip?
I am running Postgresql 9.1 on ububtu
I used: psql -d testdb -U postgres
I can see the tables in testdb, among them tables I created earlier, however when trying:
create table newtable(name VARCHAR(10)...);
i get nothing. No CREATE result, and consequently do not see the new table created.
I am new to this db, would appreciate help - what is the problem here?
thnx