Is there a way in which I can get the table definition in a script that I can execute?
i.e.
I have a table "cities":
CREATE TABLE public.cities
(
name character(80) COLLATE pg_catalog."default" NOT NULL,
location point,
CONSTRAINT pk_city_name PRIMARY KEY (name)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.cities
OWNER to postgres;
is there a way I can generate that with a script rather than using the GUI?
If you want to write your commands into a script file, and the run it from command line, you should use psql -f <filename>.
See https://www.postgresql.org/docs/9.2/static/app-psql.html
Thanks for the links and places to look.
for those reading what I done:
open cmd
navigate to C:\Program Files\PostgreSQL\10\bin> (or create a short cut)
pg_dump -d mydb -t cities -U postgres -h localhost > C:/test/weather.sql
enter password for postgres
file is output to directory.
Related
I m using Docker container it runs scripts in order 1-db.sql, 2-schemas.sql, 3-tables.sql located in db_init directory
1-db.sql looks like:
CREATE DATABASE my_db;
2-schemas.sql
CREATE SCHEMA my_db.credentials
AUTHORIZATION user1;
3-tables.sql
CREATE TABLE my_db.credentials."user_credentials"
(
id integer NOT NULL,
username character varying(40) NOT NULL,
password character varying(120) NOT NULL,
PRIMARY KEY (id)
);
Docker command i use (excluding password variables and port mappings) is:
docker run --name $CONTAINER_NAME -v $PWD/db_init/:/docker-entrypoint-initdb.d/ -d -t kartoza/postgis
I copy those files to correct directory and they are executed in correct order, however I get messages 'cross-database references are not implemented'
How to specify so that all of that corresponds to same database my_db (it was added to first database gis)?
I wanted to run the alter table command using bash script. I managed to create the table, load the basemodel, create config tables and etc. The script will login to the postgres database before it is execute the alter table command. It stuck as (abcdb=> ) without proceed to the alter table command. Is there any way to make sure the alter table able to execute?
The login as
psql -h 191.169.51.10 -d abcdb -U myname
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
In order to run a script like this you need to redirect the SQL/DML (alter table statements) into the psql command. Otherwise bash won't understand what to do with them.
psql -h 191.169.51.10 -d abcdb -U myname << EOF
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
EOF
Alternatively you can put your SQL/DML into a separate file and have psql to read from that:
psql -h 191.169.51.10 -d abcdb -U myname < alter_statements.sql
Or
psql -h 191.169.51.10 -d abcdb -U myname -f alter_statements.sql
So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema.
The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.
CREATE ROLE "new_role" ... ;
CREATE TABLESPACE "new_space"
OWNER "new_role"
LOCATION '/home/...';
CREATE DATABASE "new_db"
WITH OWNER = "new_role"
TABLESPACE = "new_space";
CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
CREATE TABLE IF NOT EXISTS "schema"."new_table"(
...
) TABLESPACE "new_space";...
...
I already saw a solution with a \connect foo; but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running \connect foo in the terminal.
Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?
Use psql and split it up into two scripts . You can save the scripts in .sql files, and then run psql to connect to the DB you want to run each script against all on the same command line (with && in between each command). The two psql commands could be combined into one bash script so it's only one command that you need to run.
Something like this, if the script were named foo.sql:
psql -X -h <host> -U <user> -p <port> -f foo.sql <db_name>
The first script could have the create role, create tablespace and create database commands, connecting to the postgres db or a template DB, and the second script could have the rest of the commands.
You could also use createdb from the bash script instead of CREATE DATABASE.
Using pgAdminIV:
1- right click on default database "postgres"
2- select create database, give a name f.e. "newdatabase"
3- click on "newdatabase" (to establish connection)
4- open the query tool
5- import, write or paste your code
6- run your code f.e.: CREATE SCHEMA newschema;
It works for me...
Hi there if anyone can help me, I have a .sh script that executes 4 .sql scripts, each executing against a schema. Currently the schema name is hardcoded but i want to make it configurable.
Given the following below how will i pass the arguments from the shell script to the .files?
an e.g call to a .sql is done in my shell script is done so like the following
ECHO “DELETING SCHEME….”
psql -f $SCRIPT_DIR/delete_data.sql my_db postgres
ECHO “DATABASE SCHEMA DELETED..”
delete_data.sql
drop schema my_schema cascade;
create schema my_schema;
You could replace the my_schema part with a placeholder, like %SCHEMA%:
drop schema %SCHEMA% cascade;
create schema %SCHEMA%;
We then run a substitution using sed, and pipe the results into psql (reading from stdin is equivalent to reading from file):
sed "s/%SCHEMA%/$schemaName/" $SCRIPT_DIR/delete_data.sql | psql powa_aim_db postgres
You can do this using a heredoc for your SQL:
my_schema="$1"
ECHO “DELETING SCHEME….”
psql <<SQL
drop schema $my_schema cascade
create schema $my_schema
SQL
ECHO “DATABASE SCHEMA DELETED..”
Then call your script with the schema name as the first argument:
$ ./my_script my_schema_name
I created a table visually in DB2 Control Center. Is there a way or command in DB2 to show the SQL source for the create-table, after it's created?
There is a DB2-tool called db2look (command line) that can generate the DDL scripts for the creation of all the database objects (tables, views, constraints, ...). Have a look at the command options to let it create the DDLs of just 1 table and its constraints.
db2look -d YourDatabaseName -e -t YourTableName -o YourTableName.sql